javascript JSONP 使用 JQuery 从 HTTPS 协议中获取 JSON

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3780702/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 02:13:05  来源:igfitidea点击:

JSONP To Acquire JSON From HTTPS Protocol with JQuery

javascriptjqueryajaxjsonjsonp

提问by Doug Molineux

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

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

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

客户端希望不使用任何服务器端语言(全部使用 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.

我已经读到,当使用 Jquery 的 .ajax 函数时,我必须使用 JSONP 才能从安全站点加载 JSON。

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

我的第一个问题是我需要将此 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);

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.

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

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

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!

我还收到一条错误消息,指出我的 $.ajax 请求是“未定义的”,但我可以在响应中看到数据。我怀疑这与我获取初始数据的方式有关。任何意见,将不胜感激。谢谢!

采纳答案by trrrrrrm

you can use getJSON for example

例如,您可以使用 getJSON

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

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

EDIT

编辑

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

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

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

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

Here is an example of cross-domain get JSON

下面是一个跨域获取JSON的例子

EDIT

编辑

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

Firefox 的 HTTPS 有问题,我知道如果您像这样发送请求,它将被修复

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

来源Firefox 中使用 jquery 的 AJAX https POST 请求失败

Hope this helps

希望这可以帮助