简体   繁体   English

如何使用jsonp从跨域获取响应

[英]How to get response from cross domain using jsonp

I am working on AJAX login on cross domain and request has been sent correctly and getting response from other domain but my onSuccess function is not getting called. 我正在跨域上进行AJAX登录,并且请求已正确发送并从其他域获得响应,但未调用我的onSuccess函数。 I have tried writing onsuccess inside request like 我尝试在请求中写onsuccess

sucess : function(){}

but it is also not getting called. 但它也没有被调用。

My code snippet : 我的代码段:

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

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

-Thanx. 谢谢

Your request looks good, according to the documentation. 根据文档,您的请求看起来不错。 From README.md README.md

Handling Failures 处理失败

Since there is no way to inspect what happens after we make a request with the JSONP technique, we're stuck having to make informed guesses about what's going on. 由于无法检查使用JSONP技术发出请求后发生的情况,因此,我们不得不对发生的情况做出明智的猜测。

This example makes a request to an invalid URL. 本示例向无效的URL发出请求。 Since the callback is not invoked within the default timeout period (10 seconds) the request is "cancelled" and the onFailure callback is invoked if specified. 由于未在默认超时时间(10秒)内调用该回调,因此该请求被“取消”,并且如果已指定,则调用onFailure回调。 The Ajax.JSONResponse will have the status of 504 and statusText of "Gateway Timeout". Ajax.JSONResponse将具有状态504和statusText为“网关超时”。

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);
  }
});

So, you should add the additional callbacks, to see what's going wrong. 因此,您应该添加其他回调,以查看发生了什么问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM