在 JavaScript 中检索 URL 字符串以生成 JSON 对象?
我想访问 Yelp API 1.0,例如我有这个 URL
这将为我提供洛杉矶的一些汉堡店位置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
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您进入了同源政策的主题,它只是说您无法到达来自 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.
将“&callback=myFunction”添加到 URL。将此 url 作为源,将脚本标记附加到您的文档。
然后在页面中创建一个名为 myFunction 的函数,其第一个参数将是返回的数据。
Add "&callback=myFunction" to the url. Append a script tag to your document with this url as the source.
Then make a function in your page called myFunction, and its first argument will be the data returned.
使用 $.getJSON()。
这是使用 yelp 的示例:
Use $.getJSON().
Here's an example using yelp: