简体   繁体   English

差异jsonp和简单获取请求(跨域)

[英]Difference jsonp and simple get request (cross domain)

I have to send (and receive) certain data to a server using JQuery and JSON. 我必须使用JQuery和JSON将某些数据发送(并接收)到服务器。 Works so far, but not cross domain, and it has to be cross domain. 到目前为止工作,但不跨域,它必须是跨域的。

I looked how to solve this and found JSONP. 我看了解如何解决这个问题并找到了JSONP。 As far as I see, using JSONP I have to send the callback and the data using GET (JQuery allows to use "POST" as method, but when I inspect web traffic I see it is sending actually GET and everyting as parameter). 据我所知,使用JSONP我必须使用GET发送回调和数据(JQuery允许使用“POST”作为方法,但是当我检查网络流量时,我看到它实际发送GET并且每个都作为参数)。

JSONP also requires changes in the server, because they are expecting a POST request with JSON data, and they have to implement something to process the JSONP GET request. JSONP还需要对服务器进行更改,因为他们期望使用JSON数据发出POST请求,并且他们必须实现一些处理JSONP GET请求的内容。

So I'm wondering what's the difference between this and sending the data as key value parameters in GET request? 所以我想知道这与将数据作为GET请求中的关键值参数发送有什么区别?

Is the difference the possibility to use a callback? 是否可以使用回调? Or what exactly? 或究竟是什么?

Sorry a bit lost... thanks in advance 抱歉有点失落......先谢谢

JSONP is not a form submission. JSONP不是表单提交。 It is a way of telling the server via a GET request how to generate the content for a script tag. 这是一种通过GET请求告诉服务器如何为脚本标记生成内容的方法。 The data returned is a payload of JavaScript (not just JSON!) with a function call to a callback which you (by convention) reference in the GET request. 返回的数据是JavaScript的有效负载(不仅仅是JSON!),其中有一个函数调用回调函数(按照惯例)在GET请求中引用。

JSONP works because it is a hack that doesn't use AJAX. JSONP的工作原理是因为它是一个不使用AJAX的hack。 It isn't AJAX and you should not confuse it for such because it does not use a XMLHttpRequest at any point to send the data. 它不是AJAX,你不应该混淆它,因为它不会在任何时候使用XMLHttpRequest来发送数据。 That is how it gets around the Same Origin Policy. 这就是它绕过同源策略的方式。

Depending on the browsers you have to support, you can implement Cross-Origin Resource Sharing headers on the server side which will let you use normal AJAX calls across trusted domains. 根据您必须支持的浏览器,您可以在服务器端实现跨源资源共享标头,这将允许您跨受信任域使用普通的AJAX调用。 Most browsers (IE8, Firefox 3.5+, etc.) will support CORS. 大多数浏览器(IE8,Firefox 3.5+等)都支持CORS。

Another solution you can use if you don't want to use CORS or JSONP is to write a PHP script or Java servlet that will act as a proxy. 如果您不想使用CORS或JSONP,可以使用的另一个解决方案是编写将充当代理的PHP脚本或Java servlet。 That's as simple as opening a new connection from the script, copying all of the incoming parameters from your AJAX code onto the request and then dumping the response back at the end of your script. 这就像从脚本打开一个新连接一样简单,将所有传入的参数从AJAX代码复制到请求中,然后在脚本结束时将响应转储回来。

I found an answer that worked for me with the cross-domain issue and JSON (not JSONP). 我找到了一个适用于跨域问题和JSON(而不是JSONP)的答案。 I simply used: 我只是用过:

header('Access-Control-Allow-Origin: *');

inside my json file (file.php) and called it like this: 在我的json文件(file.php)中,并像这样调用它:

var serviceURL = 'http://your-domain.com/your/json/location.php'
$.getJSON(serviceURL,function (data) {
   var entries = data;
   //do your stuff here using your entries in json
});

BTW: this is a receiving process, not sending. 顺便说一句:这是一个接收过程,而不是发送。

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

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