【问题标题】:How to get response from cross domain using jsonp如何使用 jsonp 从跨域获取响应
【发布时间】:2013-02-13 14:06:23
【问题描述】:

我正在跨域进行 AJAX 登录,请求已正确发送并从其他域获得响应,但我的 onSuccess 函数没有被调用。 我试过在请求中写 onsuccess 像

sucess : function(){}

但它也没有被调用。

我的代码 sn-p:

new Ajax.JSONRequest(this.preCheckUrl, {
    callbackParamName: "jsoncallback",
    parameters: {
    userEmail: this.userEmail, userPassword: this.userPassword, format: 'json'
  },

  onSuccess:function(response) {
        alert('hello');
  } });

-谢谢。

【问题讨论】:

    标签: ajax json cross-domain jsonp


    【解决方案1】:

    根据文档,您的请求看起来不错。 来自README.md

    处理故障

    由于无法检查我们使用 JSONP 技术发出请求后发生的情况,因此我们不得不对正在发生的事情做出明智的猜测。

    此示例向无效 URL 发出请求。由于在默认超时期限(10 秒)内未调用回调,因此请求被“取消”,如果指定,则调用 onFailure 回调。 Ajax.JSONResponse 的状态为 504,statusText 为“Gateway Timeout”。

    new Ajax.JSONRequest('http://api.flickr.com/services/feeds/asdfasdfasdfasdfasdfsdf', {
      callbackParamName: "jsoncallback",
      parameters: {
        tags: 'cat', tagmode: 'any', format: 'json'
      },
      onCreate: function(response) {
        console.log("2: create", response, response.responseJSON);
      },
      onSuccess: function(response) {
        console.log("2: success", response, response.responseJSON);
      },
      onFailure: function(response) {
        console.log("2: fail", response, response.responseJSON);
      },
      onComplete: function(response) {
        console.log("2: complete", response, response.responseJSON);
      }
    });
    

    所以,你应该添加额外的回调,看看出了什么问题。

    【讨论】:

      猜你喜欢
      • 2014-08-14
      • 2011-03-05
      • 2015-05-31
      • 2012-08-22
      • 2013-11-25
      • 2014-05-15
      • 1970-01-01
      • 1970-01-01
      • 2011-09-01
      相关资源
      最近更新 更多