简体   繁体   English

如何使用jQuery从AJAX请求中获取响应文本

[英]How to get the response text from an AJAX request using jQuery

I am trying to make a JSONP request to a server. 我正在尝试向服务器发出JSONP请求。 This is my code: 这是我的代码:

$.ajax({
  type: 'GET',
  url: myURL,
  async: false,
  crossDomain: true,
  dataType: 'jsonp',
  jsonpCallback: 'jsonCallback',
  headers: { 
    'Accept': 'application/json', //this is required by the server
    'key': key   
  },
  success: function() {
    alert('1');
  },
  error: function() {
    alert('2');
  },
  complete: function(){
    alert('3');
  }
});//code indentation

When I run the code it errors. 当我运行代码时会出错。 But if I open the developers tools in Chrome (ctrl+shift+I) I can see the request under "network". 但是,如果我在Chrome(ctrl + shift + I)中打开开发人员工具,则可以在“网络”下看到请求。 Clicking on it shows the correct response (and shows the request was accepted). 单击它显示正确的响应(并显示请求已被接受)。

Apologies is there is a really obvious solution (I have tried searching, but with no luck), but at this point I am well and truly baffled. 道歉是一个非常明显的解决方案(我曾尝试过搜索,但是没有运气),但在这一点上,我感到非常困惑。 Any help would be really appreciated. 任何帮助将非常感激。

::EDIT:: ::编辑::

changing the error function to: 将错误函数更改为:

error: function() {
  console.log('error', arguments);
},

returned the message "jsonCallback was not called" Thanks to Aaron Digulla below. 由于下面的Aaron Digulla,返回了消息“未调用jsonCallback”。

The response from the server is JSON, not JSONP (checked with JSONlint) 来自服务器的响应是JSON,而不是JSONP(已通过JSONlint检查)

When you say "it errors", my guess is that you get alert(2) . 当您说“它出错”时,我的猜测是您得到alert(2) To find out why, log the function arguments to the console: 要找出原因,请将函数参数记录到控制台:

...
error: function() {
    console.log('error', arguments);
},
...

jQuery will pass additional information (like the error message) to the function. jQuery会将其他信息(例如错误消息)传递给该函数。 That should help you understand why it fails. 那应该可以帮助您了解其失败的原因。

The same is true for the success function which gets the server response, for example. 例如,对于success功能,获取服务器响应的情况同样如此。

[EDIT] [编辑]

I get the error jsonCallback was not called 我收到jsonCallback was not called的错误

That means your server isn't returning JSONP. 这意味着您的服务器没有返回JSONP。 JSONP looks like name({...}) while normal JSON looks like {...} . JSONP看起来像name({...})而普通的JSON看起来像{...} Please check your server's configuration and make sure it actually supports JSONP and that the response looks correct. 请检查您服务器的配置,并确保它实际上支持JSONP,并且响应看起来正确。

I should have seen this from your code: 我应该已经从您的代码中看到了这一点:

dataType: 'jsonp'
headers: { 
    'Accept': 'application/json', //this is required by the server
}

That means you're sending a JSONP/JSON mix which can't work. 这意味着您正在发送无效的JSONP / JSON组合。 If you use a certain dataType , then let jQuery build the correct headers. 如果您使用特定的dataType ,那么让jQuery构建正确的标头。

The success function has argument and from that argument you can get the response text. 成功函数具有参数,您可以从该参数获取响应文本。

$.ajax({
  type: 'GET',
  url: myURL,
  async: false,
  crossDomain: true,
  dataType: 'jsonp',
  jsonpCallback: 'jsonCallback',
  headers: { 
    'Accept': 'application/json', //this is required by the server
    'key': key 
  },
  success: function(response) {
    alert(response);
    alert('1');
  },
  error: function() {
    alert('2');
  },
  complete: function(){
    alert('3');
  }
});

您无法为jsonp请求设置async: false ,因为其性质如此,因此添加了脚本标记以处理响应方法。

 dataType: 'jsonp

如您所述,您希望从服务器返回的数据类型为jsonp,但可能是您的服务器将返回任何其他格式,而不是jsonp ..因此,请在浏览器的“网络”下检查服务器返回的响应类型控制台...如果不是jsonp格式,请更改您的响应返回类型...。

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

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