jQuery,ajax和jsonp的问题

时间:2022-12-08 19:34:16

I am using jsonp and ajax to access a web service on another server. Here's the jQuery:

我正在使用jsonp和ajax访问另一台服务器上的Web服务。这是jQuery:

$.ajax({
  type: 'GET',
  url: wsurl + 'callback=?',
  dataType: 'jsonp',
  crossDomain: true,
  error: function(data) {
    console.log('error', data);
  },
  success: function(data) {
    console.log('success', data);
  },
  complete: function() {
    console.log('done');
  }
});

The issue is that the error callback is being called. It gives me this wonderfully helpful information:

问题是正在调用错误回调。它给了我这个非常有用的信息:

{
  readyState: 4,
  status: 200,
  statusText: "success"
}

And here is the json file I am calling:

这是我正在调用的json文件:

{
  "id": 0,
  "room_number": "0",
  "first_name": "Admin",
  "last_name": "Istrator",
  "password": "",
  "salutation": "Mr.",
  "telephone": "",
  "email": "",
  "description": "admin",
  "checkin_date": 915797106000,
  "checkout_date": 4071557106000,
  "last_login_date": 947333106000,
  "active_status": true,
  "created_date": 915797106000,
  "created_by": 0,
  "reference_id": ""
}

I tried using the getJSON jQuery method first, with the same result. Thought I'd try the base ajax method, since it has a bit more control, but as you can see, no luck. So, notice anything I'm doing wrong? Have any idea why it is throwing an error and giving me a successful value for the statusText property?

我首先尝试使用getJSON jQuery方法,结果相同。以为我会尝试基础ajax方法,因为它有更多的控制权,但正如你所看到的,没有运气。那么,请注意我做错了什么?知道为什么它会抛出一个错误并给我一个statusText属性的成功价值吗?

EDIT

Alright, I added some options to the ajax call, and removed the callback param from the url. Here's the new ajax call:

好吧,我在ajax调用中添加了一些选项,并从url中删除了回调参数。这是新的ajax调用:

  $.ajax({
    type: 'GET',
    url: wsurl,
    dataType: 'jsonp',
    crossDomain: true,
    error: function(xhr, textStatus, errorThrown) {
      console.log('textStatus: ' + textStatus);
    },
    success: function(data) {
      console.log('success');
      console.log(data);
    }
  });

I'm getting a new error, which is good I suppose, but still not working. Difference is that the textStatus is now "parsererror". The console is also throwing a syntax error on line one of the json file:

我收到了一个新的错误,我想这很好,但仍然无法正常工作。区别在于textStatus现在是“parsererror”。控制台也在json文件的第一行抛出语法错误:

Uncaught SyntaxError: Unexpected token :

Ideas?

想法?

3 个解决方案

#1


1  

Okay a couple of things jump out at me:

好吧有几件事情向我跳出来:

$.ajax({
  type: 'GET',
  #You do not need to append the callback as you have jsonp configured it will  do it    
  #automatically append the callback=<auto generated name>
  url: wsurl, 
  dataType: 'jsonp',
  crossDomain: true,
  error: function(data) {
    console.log('error', data);
  },
  success: function(data) {
    console.log('success', data);
  },
  complete: function() {
    console.log('done');
  }
});

Also your return does not to appear to be wrapped in a function which is required for jsonp to work.

此外,您的返回似乎不会包含在jsonp工作所需的函数中。

<auto generated name>({ json object })

The callback function will be named by jquery automatically. So you need a service that will take in a callback parameter and return a json object with padding.

回调函数将由jquery自动命名。所以你需要一个服务来接受一个回调参数并返回一个带填充的json对象。

#2


0  

Not sure if this is the problem, but your url is not correct. According to the jQuery docs, it should automatically append the ?callback=? for you:

不确定这是否是问题,但您的网址不正确。根据jQuery文档,它应该自动附加?callback =?为你:

http://api.jquery.com/jQuery.ajax/

http://api.jquery.com/jQuery.ajax/

#3


0  

Remove the callback from the URL and try and wrap your json with something like this (php example, $_GET['callback'] is automatically set by jQuery):

从URL中删除回调并尝试用这样的东西包装json(php示例,$ _GET ['callback']由jQuery自动设置):

$_GET['callback'] . '({
  "id": 0,
  "room_number": "0",
  "first_name": "Admin",
  "last_name": "Istrator",
  "password": "",
  "salutation": "Mr.",
  "telephone": "",
  "email": "",
  "description": "admin",
  "checkin_date": 915797106000,
  "checkout_date": 4071557106000,
  "last_login_date": 947333106000,
  "active_status": true,
  "created_date": 915797106000,
  "created_by": 0,
  "reference_id": ""
})'

#1


1  

Okay a couple of things jump out at me:

好吧有几件事情向我跳出来:

$.ajax({
  type: 'GET',
  #You do not need to append the callback as you have jsonp configured it will  do it    
  #automatically append the callback=<auto generated name>
  url: wsurl, 
  dataType: 'jsonp',
  crossDomain: true,
  error: function(data) {
    console.log('error', data);
  },
  success: function(data) {
    console.log('success', data);
  },
  complete: function() {
    console.log('done');
  }
});

Also your return does not to appear to be wrapped in a function which is required for jsonp to work.

此外,您的返回似乎不会包含在jsonp工作所需的函数中。

<auto generated name>({ json object })

The callback function will be named by jquery automatically. So you need a service that will take in a callback parameter and return a json object with padding.

回调函数将由jquery自动命名。所以你需要一个服务来接受一个回调参数并返回一个带填充的json对象。

#2


0  

Not sure if this is the problem, but your url is not correct. According to the jQuery docs, it should automatically append the ?callback=? for you:

不确定这是否是问题,但您的网址不正确。根据jQuery文档,它应该自动附加?callback =?为你:

http://api.jquery.com/jQuery.ajax/

http://api.jquery.com/jQuery.ajax/

#3


0  

Remove the callback from the URL and try and wrap your json with something like this (php example, $_GET['callback'] is automatically set by jQuery):

从URL中删除回调并尝试用这样的东西包装json(php示例,$ _GET ['callback']由jQuery自动设置):

$_GET['callback'] . '({
  "id": 0,
  "room_number": "0",
  "first_name": "Admin",
  "last_name": "Istrator",
  "password": "",
  "salutation": "Mr.",
  "telephone": "",
  "email": "",
  "description": "admin",
  "checkin_date": 915797106000,
  "checkout_date": 4071557106000,
  "last_login_date": 947333106000,
  "active_status": true,
  "created_date": 915797106000,
  "created_by": 0,
  "reference_id": ""
})'