JSONP 使用 JQuery 从 HTTPS 协议获取 JSON

发布于 2024-09-24 15:51:56 字数 861 浏览 0 评论 0原文

我正在尝试获取从 https 安全站点发送的 JSON,

客户端希望不使用任何服务器端语言(整个内容都在 Javascript 中)

我读到我必须使用 JSONP 才能加载使用 Jquery 中的 .ajax 函数时,来自安全站点的 JSON。

我的第一个问题是我需要将此 JSONP 设置为什么格式?现在我的代码如下所示:

html =new Object();
    html = $.ajax({
      url: "https://my-secure.net",
      async: false,
      dataType: 'jsonp'
     }).responseText;

//alert(html);       
  alert("myObject is " + html.toSource());
     console.log(html);

但是,没有任何警报,Firebug 中也没有记录任何内容。基本上我希望能够操作 JSON 数据。我在 Firebug 下的响应中看到了数据,但有一个错误,提示“标签无效”。我读过,为了解决这个问题,您将其包含在带有额外括号的 eval 函数中,但这不起作用。

http://b.lesseverything.com/2007 /10/25/invalid-label-error-when-eval-json

我还收到一个错误,表示我的 $.ajax 请求“未定义”,但我可以看到响应中的数据。我怀疑这与我获取初始数据的方式有关。任何建议将不胜感激。谢谢你!

I'm trying to acquire a JSON which is being sent from an https secure site,

The client was hoping not to use any Server-side languages (the whole thing in Javascript)

I've read that I must use JSONP in order to load a JSON from a secure site, when using the .ajax function from Jquery.

My first question would be what format do I need to set this JSONP as? Right now my code looks like this:

html =new Object();
    html = $.ajax({
      url: "https://my-secure.net",
      async: false,
      dataType: 'jsonp'
     }).responseText;

//alert(html);       
  alert("myObject is " + html.toSource());
     console.log(html);

However, nothing is being alerted, nor is anything being logged in Firebug. Basically I want to be able to manipulate the JSON data. I see the data in the Response under Firebug, but there's an error which says "invalid label." I've read that in order to fix this you encase it in the eval function with extra parantheses but this is not working.

http://b.lesseverything.com/2007/10/25/invalid-label-error-when-eval-json

I also get an error which says my $.ajax request is "undefined" but I can see the data in the response. I suspect this has something to do with how I am grabbing the initial data. Any advice would be appreciated. Thank you!

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

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

发布评论

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

评论(1

内心荒芜 2024-10-01 15:51:56

您可以使用 getJSON 例如

$.getJSON('ajax/test.json', function(data) {
  $('.result').html('<p>' + data.foo + '</p>'
    + '<p>' + data.baz[1] + '</p>');
});

检查完整的 getJSON 文档 http://api.jquery.com/jQuery.getJSON /

编辑

我错了...使用 Jquery.ajax 会导致跨浏览器问题,但使用 Jquery.getJSON 则不会

http://docs.jquery.com/Release:jQuery_1.2/Ajax#Cross-Domain_getJSON_.28using_JSONP.29

这是跨域获取 JSON 的示例

编辑

Firefox 在 HTTPS 方面存在问题,因为我知道如果您像这样

$.getJSON('ajax/test.json',{}, function(data) {
  $('.result').html('<p>' + data.foo + '</p>'
    + '<p>' + data.baz[1] + '</p>');
});

来源发送请求,问题将会得到解决: 使用 jquery 的 AJAX https POST 请求在 Firefox 中失败

希望这有帮助

you can use getJSON for example

$.getJSON('ajax/test.json', function(data) {
  $('.result').html('<p>' + data.foo + '</p>'
    + '<p>' + data.baz[1] + '</p>');
});

check complete getJSON documentation http://api.jquery.com/jQuery.getJSON/

EDIT

I was wrong... using Jquery.ajax will cause cross-browser issue but not with Jquery.getJSON

http://docs.jquery.com/Release:jQuery_1.2/Ajax#Cross-Domain_getJSON_.28using_JSONP.29

Here is an example of cross-domain get JSON

EDIT

Firefox has a problem with HTTPS, as i know it will be fixed if you send your request like this

$.getJSON('ajax/test.json',{}, function(data) {
  $('.result').html('<p>' + data.foo + '</p>'
    + '<p>' + data.baz[1] + '</p>');
});

Source: AJAX https POST requests using jquery fail in Firefox

Hope this helps

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