jQuery AJAX URL 缩短器 JSON 请求不起作用

发布于 2024-09-15 22:33:56 字数 489 浏览 3 评论 0原文

我被困住了 - 对于我认为很简单的事情来说,这非常令人沮丧:(

Jquery:

$.ajax({ 
 url: "http://ur.ly/new.json?href=http://www.yahoo.com"), 
 type: "GET",
 dataType: "jsonp",
 success: function(data){
 console.log(data);
}});

我已经尝试这样做,FF 给了我一个“无效标签”错误,Chrome 给出了另一个错误。

当我使用“json”时作为数据类型,

令人沮丧的是,当您在浏览器上尝试时,URL 工作正常,并且返回数据的“jsonp”变量的错误显示了我想要的数据,但是。错误阻止我访问它。

我尝试切换到 XML 或脚本,相应地更改 API 的要求,但我尝试过其他缩短站点,但同样的事情不断发生

。 ,但它也不起作用

:(感谢您的浏览。:)

I'm stuck - and it's pretty frustrating for something I thought was going to be simple :(

Jquery:

$.ajax({ 
 url: "http://ur.ly/new.json?href=http://www.yahoo.com"), 
 type: "GET",
 dataType: "jsonp",
 success: function(data){
 console.log(data);
}});

I've tried doing this, and FF gives me an "invalid label" error, Chrome gives another.

When I use "json" as the data type, I get a null return.

The frustrating thing is that the URL, when you try it on the browser, works fine. And the error from the "jsonp" variable for the returned data shows the data I want, but errors prevent me from getting to it.

I've tried swapping to XML or Script, changing the API's requirements accordingly, but nothing. I've tried other shortening sites but the same thing keeps happening.

Also tried porting the query part to data:, but it didn't work either.

HELP! :( And thanks for looking through. :)

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

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

发布评论

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

评论(2

夜还是长夜 2024-09-22 22:33:56

jsonp 是正确的。

出于安全原因,任何其他类型都不起作用。


我尝试了您的代码,ur.ly 的结果是纯 json 字符串而不是 jsonp 字符串:

{"code":"X5","href":"http://www.yahoo.com"}

您可以编写一个小型服务器端脚本来读取这些数据。

然后,您的 JavaScript 可以使用 json 而不是 jsonp

jsonp is correct.

Any other type won't work for security reasons.


I tried your code an the result from ur.ly is a pure json string not a jsonp string:

{"code":"X5","href":"http://www.yahoo.com"}

You might write a small server side script to read those data.

Your javascript could then use json instead of jsonp.

野生奥特曼 2024-09-22 22:33:56

这是因为您尝试使用 jQuery AJAX 调用发出 JSONP 请求,但 ur.ly API 目前不支持 JSONP。由于 Javascript 的 同源政策,对不同域使用纯 JSON 调用将永远无法工作。

您必须创建一个服务器端代理脚本作为 JavaScript 和远程服务器的中间人(正如 Ghommey 上面所说),或者使用确实支持的不同 URL 缩短 API JSONP 请求(例如 bit.ly)。

This is because you are trying to make JSONP requests with your jQuery AJAX calls, but the ur.ly API doesn't currently support JSONP. Using plain JSON calls to a different domain won't ever work, due to Javascript's same origin policy.

You will either have to create a server-side proxy script that acts as a go-between for your JavaScript and the remote server (as Ghommey says above), or use a different URL shortening API that does support JSONP requests (e.g bit.ly).

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