在 JavaScript 中检索 URL 字符串以生成 JSON 对象?

发布于 2024-12-17 10:28:54 字数 728 浏览 3 评论 0原文

我想访问 Yelp API 1.0,例如我有这个 URL

http://api.yelp.com/business_review_search?term=burger&location=Los%20Angeles%2A%20CA&ywsid=SECRETKEY

这将为我提供洛杉矶的一些汉堡店位置JSON 字符串。

我如何访问此 URL 以便将响应转换为 JS 对象?

我知道如何创建 JS 对象,问题是从服务器获取响应。

这是我现在的代码:也许我们可以尝试从那里开始:

var url = "http://api.yelp.com/business_review_search?term=burger&location=Los%20Angeles%2A%20CA&ywsid=secretKey" ;


            $.getJSON(url, function(json) {
                alert("JSON Data: " + json.message.text);
            });

I want to access the Yelp API 1.0, I have got for example this URL

http://api.yelp.com/business_review_search?term=burger&location=Los%20Angeles%2A%20CA&ywsid=SECRETKEY

which will give me some Burger Locations in LA in a JSON String.

How can I access this URL in order turn the response into a JS Object?

I know how to create JS Objects, the problem is getting the response from the server.

Here's my code right now: Maybe we can try to start from there:

var url = "http://api.yelp.com/business_review_search?term=burger&location=Los%20Angeles%2A%20CA&ywsid=secretKey" ;


            $.getJSON(url, function(json) {
                alert("JSON Data: " + json.message.text);
            });

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

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

发布评论

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

评论(3

扎心 2024-12-24 10:28:54

您进入了同源政策的主题,它只是说您无法到达来自 b.com 的 URL 来自 a.com,使用正常形式的 ajax。

如果您使用ajax,则应该使用JSONP,或访问控制标头。否则,您应该使用您的服务器作为代理来将常规 HTTP GET 和 POST 发送到该 URL。

You get into the subject of Same-Origin Policy, which simply says that you can't reach a URL from b.com from a.com using ajax in a normal form.

If you use ajax, you should use JSONP, or Access-Control headers. Otherwise, you should use your server as a proxy to send regular HTTP GET and POST to that URL.

我的黑色迷你裙 2024-12-24 10:28:54

将“&callback=myFunction”添加到 URL。将此 url 作为源,将脚本标记附加到您的文档。

var scriptElem = document.createElement('script');
scriptElem.src = url;
document.body.appendChild(scriptElem);

然后在页面中创建一个名为 myFunction 的函数,其第一个参数将是返回的数据。

Add "&callback=myFunction" to the url. Append a script tag to your document with this url as the source.

var scriptElem = document.createElement('script');
scriptElem.src = url;
document.body.appendChild(scriptElem);

Then make a function in your page called myFunction, and its first argument will be the data returned.

私藏温柔 2024-12-24 10:28:54

使用 $.getJSON()

这是使用 yelp 的示例:

$.getJSON("http://api.yelp.com/business_review_search?term=burger&location=Los%20Angeles%2A%20CA&ywsid=SECRETKEY&jsoncallback=?", function(json) {
   console.log("JSON Data: " + json);
});

Use $.getJSON().

Here's an example using yelp:

$.getJSON("http://api.yelp.com/business_review_search?term=burger&location=Los%20Angeles%2A%20CA&ywsid=SECRETKEY&jsoncallback=?", function(json) {
   console.log("JSON Data: " + json);
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文