测试静态 jsonp 响应

发布于 2024-10-06 12:32:07 字数 1048 浏览 0 评论 0原文

我在发出 jsonp 请求时没有遇到任何问题,但是我不确定是否设置 Web 服务以在 jsonp 中传递响应。

首先,服务器是否需要以某种方式配置以允许 jsonp 请求,或者页面只需要正确格式化响应?

在我的测试中,我收到了来自 geonames.org 的以下 jsonp 响应(我已将其放置在服务器/域 1 上的空白页面,没有其他任何内容):

<?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 上,我发出以下请求:

$.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 请求。我做错了什么?

只是为了澄清:我的问题与接收请求的页面有关。我知道当我发送到 geonames.org、flickr 等 api 时该请求有效。但是,我正在尝试设置一个页面来发送响应。在我的示例中,我只有一个带有硬编码 jsonp 的空白页面。我不确定页面上是否必须有其他标题或在我的服务器上启用了某些功能。

I have no trouble making jsonp requests, however I'm unsure about setting up a web service to deliver responses in 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?

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"}]});

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);
    },
});

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?

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.

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

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

发布评论

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

评论(2

写下不归期 2024-10-13 12:32:07

响应是错误的。

如果您有以下网址:
http://www.mydomain.com/test/jsonp.php&callback=? jQuery 将用唯一的字符串替换 url 末尾的问号。在服务器端,您必须采用此字符串($_GET['callback'])并将其用作响应中的函数名称:

PHP-example:

<?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 创建一个

因此,如果您不在响应中使用 jQuery 提供的回调参数作为函数名称,则 jQuery 不知道要调用的函数的名称(我最好说 jQuery 将调用一个不存在的函数)。

The response is wrong.

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

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

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

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

日记撕了你也走了 2024-10-13 12:32:07

我一直使用 $.getJSON() 代替 $.ajax 进行跨浏览器请求所以我不知道使用 $.ajax 调用 json 的具体细节,但您是否尝试过将 dataType 设置为 jsonp ?

另外,你试过吗?callback=?而不是 &callback=?

$.ajax({
    url: 'http://www.mydomain.com/test/jsonp.php?callback=?',
    dataType: 'jsonp',
    success: function(data) {
        $('#test').html(data);
    },
});

最后,当我将测试 json 运行到 jsonlint.com 时,它没有返回有效。它说语法错误,第 1 行出现意外的 TINVALID 解析失败

I've always used $.getJSON() in place $.ajax for cross-browser requests so I don't know the specifics on calling json using $.ajax as you have here, but have you tried setting the dataType to jsonp?

Also, have you tried ?callback=? instead of &callback=?

$.ajax({
    url: 'http://www.mydomain.com/test/jsonp.php?callback=?',
    dataType: 'jsonp',
    success: function(data) {
        $('#test').html(data);
    },
});

Lastly, when I run your test json into jsonlint.com it is not returning valid. It says syntax error, unexpected TINVALID at line 1 Parsing failed

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