测试静态jsonp响应 [英] Testing a static jsonp response
问题描述
我毫不费力地发出jsonp请求,但是我不确定要设置一个Web服务来传递jsonp中的响应.
I have no trouble making jsonp requests, however I'm unsure about setting up a web service to deliver responses in jsonp.
首先,是否需要以某种方式配置服务器以允许jsonp请求,或者页面仅必须正确设置响应的格式?
First, does a server need to be configured in a certain way to allow jsonp requests, or does the page just have to have the response properly formatted?
在测试中,我收到来自geonames.org的以下jsonp响应(我将其放在服务器/域1上的空白页面上,没有其他内容):
In my testing I have the following jsonp response from geonames.org (I've placed it a blank page on server/domain 1 with nothing else):
<?php echo $_GET['callback'];?>({"postalcodes":[{"adminName2":"Westchester","adminCode2":"119","postalcode":"10504","adminCode1":"NY","countryCode":"US","lng":-73.700942,"placeName":"Armonk","lat":41.136002,"adminName1":"New York"}]});
在服务器/域2上,我正在发出以下请求:
On server/domain 2 I'm making the following request:
$.ajax({
// works when I make the call to geonames.org instead of domain1
//url: 'http://www.geonames.org/postalCodeLookupJSON?postalcode=10504&country=US&callback=?',,
url: 'http://www.domain1.com/test/jsonp.php?callback=?',
success: function(data) {
$('#test').html(data);
},
});
当我将文件放在同一服务器(域1或2)上并将其转换为常规json请求时,该调用有效.我在做什么错了?
The call works when I place the files on the same server (either domain 1 or 2) and turn it into a regular json request. What am I doing wrong?
请澄清一下:我的问题与正在接收请求的页面有关.我知道将请求发送到geonames.org,flickr等... api时,该请求有效.但是,我正在尝试设置一个页面来发送响应.在我的示例中,我只有一个空白页面,上面带有硬编码的jsonp.我不确定页面上是否需要其他标题或服务器上是否启用了某些内容.
Just to clarify: My question pertains to the page RECEIVING the request. I know the request works when I make it to geonames.org, flickr, etc... apis. However, I'm trying to set up a page to send a response. In my example I just have a blank page with hard coded jsonp. I'm not sure if I have to have some other headers on the page or have something enabled on my server.
推荐答案
响应错误.
如果您具有以下网址: http://www.mydomain.com/test/jsonp.php&callback=? jQuery将用唯一的字符串替换网址末尾的问号.在服务器端,您必须将此字符串( $ _ GET ['callback'] )用作响应中的函数名:
If you have the following url: http://www.mydomain.com/test/jsonp.php&callback=? jQuery will replace the question mark at the end of the url with a unique string. On the serverside you have to take this string($_GET['callback']) and use it as function-name in your response:
PHP示例:
<?php
$object=array('postalcodes'
=>array(
array(
"adminName2" => "Westchester",
"adminCode2" => "119",
"postalcode" => "10504",
"adminCode1" => "NY",
"countryCode" => "US",
"lng" => -73.700942,
"placeName" => "Armonk",
"lat" => 41.136002,
"adminName1" => "New York"
)));
echo $_GET['callback'].'('.json_encode($object).')';
?>
收到响应后会发生什么? jQuery知道唯一的字符串(假设 fx123456 ).
jQuery将使用src创建一个<script>
元素: http://www.mydomain.com/test/jsonp.php&callback=fx123456 . jQuery将即时调用名为 fx123456()的函数.此函数将返回JSON(作为对象),该JSON将作为 $.ajax()的成功函数的数据参数.
What happens with the response when receiving it? jQuery knows the unique string(assuming fx123456).
jQuery will create a <script>
-element with the src: http://www.mydomain.com/test/jsonp.php&callback=fx123456 . jQuery will call a on the fly created function named fx123456() . This function will return the JSON(as a object) which will be taken as data-argument of the success-function of $.ajax().
因此,如果您不使用jQuery提供的回调参数作为响应内的functions-name,则jQuery不知道要调用的函数的名称(我最好说jQuery将调用不存在的函数).
So if you don't use the callback-parameter provided by jQuery as functions-name inside the response, jQuery doesn't know the name of function to call(I better say jQuery will call a function that doesn't exist).
这篇关于测试静态jsonp响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!