jQuery、ajax 和 jsonp 的问题

发布于 2025-01-01 08:23:51 字数 1626 浏览 0 评论 0原文

我正在使用 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');
  }
});

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

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

这是我正在调用的 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": ""
}

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

编辑

好吧,我向 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);
    }
  });

我收到一个新错误,我认为这很好,但仍然无法正常工作。不同之处在于 textStatus 现在是“parsererror”。控制台还在 json 文件的第一行抛出语法错误:

Uncaught SyntaxError: Unexpected token :

想法?

I am using jsonp and ajax to access a web service on another server. Here's the 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:

{
  "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?

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({
    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:

Uncaught SyntaxError: Unexpected token :

Ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

山田美奈子 2025-01-08 08:23:51

好吧,有几件事引起了我的注意:

$.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');
  }
});

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

<auto generated name>({ json object })

回调函数将由 jquery 自动命名。因此,您需要一个接受回调参数并返回带填充的 json 对象的服务。

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.

<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.

岁吢 2025-01-08 08:23:51

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

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

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:

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

尛丟丟 2025-01-08 08:23:51

从 URL 中删除回调并尝试用类似这样的内容包装你的 json(例如,$_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": ""
})'

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):

$_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": ""
})'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文