django - 对 jQuery 的 JSON 响应
我试图将这两部分粘合在一起,我什至不知道问题出在哪里,没有alert()并且firebug没有告诉我任何事情。
// django with yellow api to find_business search:
// ----------------------
def get_yellow(request):
mimetype = 'application/json'
yapi = YellowAPI(settings.YELLOW_API_KEY, test_mode=True, format='JSON', handlers=[])
data = yapi.find_business(what='403-253-0395', where='Calgary', uid='127.0.0.1')
print data #I can see here there is a result
return HttpResponse(data,mimetype)
// jQuery
// ----------------------
$(document).ready(function(){
$.getJSON('http://myserver:8000/get_yellow/',
function(data) {
alert('Fetched ' + data.length + ' items!');
})
});
// I'm including a full response on a simple call, is this a valid json?
// ----------------------
{“summary”:{“what”:“403-253-0395”,“where”:“卡尔加里”,“纬度”:“”,“经度”:“”,“firstListing”:1,“lastListing”: 1、《总清单》 s":1,"pageCount":1,"currentPage":1,"listingsPerPage":40},"listings":[{"parentId":"","isParent":false,"距离":"", “内容”:{“V ideo":{"avail":false,"inMkt":false},"照片":{"avail":false,"inMkt":false},"个人资料":{"avail":false,"inMkt": false},"DspAd":{"ava il":false,"inMkt":false},"Url":{"avail":false,"inMkt":false},"徽标":{"avail":false,"inMkt":false}}," id":"1931218","name":"未来商店","地址":{"街道":"1180-33 Heritage Meadows Way SE","city":"卡尔加里","prov":"AB","pcode":"T2H3B8"},"geoCode" :{"纬度":"50.987988","经度":"-114.04424"}}]}
I'm trying to glue these 2 pieces together, I don't even know where the problem is, there is no alert() and firebug does not tell me anything.
// django with yellow api to find_business search:
// ----------------------
def get_yellow(request):
mimetype = 'application/json'
yapi = YellowAPI(settings.YELLOW_API_KEY, test_mode=True, format='JSON', handlers=[])
data = yapi.find_business(what='403-253-0395', where='Calgary', uid='127.0.0.1')
print data #I can see here there is a result
return HttpResponse(data,mimetype)
// jQuery
// ----------------------
$(document).ready(function(){
$.getJSON('http://myserver:8000/get_yellow/',
function(data) {
alert('Fetched ' + data.length + ' items!');
})
});
// I'm including a full response on a simple call, is this a valid json?
// ----------------------
{"summary":{"what":"403-253-0395","where":"Calgary","latitude":"","longitude":"","firstListing":1,"lastListing":1,"totalListings":1,"pageCount":1,"currentPage":1,"listingsPerPage":40},"listings":[{"parentId":"","isParent":false,"distance":"","content":{"Video":{"avail":false,"inMkt":false},"Photo":{"avail":false,"inMkt":false},"Profile":{"avail":false,"inMkt":false},"DspAd":{"avail":false,"inMkt":false},"Url":{"avail":false,"inMkt":false},"Logo":{"avail":false,"inMkt":false}},"id":"1931218","name":"Future Shop","address":{"street":"1180-33 Heritage Meadows Way SE","city":"Calgary","prov":"AB","pcode":"T2H3B8"},"geoCode":{"latitude":"50.987988","longitude":"-114.04424"}}]}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的 JavaScript 中存在语法错误。你少了一个分号。应该是:
另外,如果您要寻找的只是调试 api 代码的方法,只需输入 url (http: //myserver:8000/get_yellow/)进入当前浏览器的地址栏。在尝试使用 javascript 调用它之前,您可能应该首先执行此操作。
更新:
如果这不是跨浏览器请求,您的调用应如下所示:
如果是跨浏览器请求,则应使用 jsonp 代替:
There's a syntax error in your javascript. You're missing a semicolon. Should be:
Also, if all you're looking for is a way to debug your api code, simply type the url (http://myserver:8000/get_yellow/) into the address bar of your current browser. You should probably be doing this first before trying to call it using javascript.
Update:
If this is not a cross browser request, your call should look like:
If it is a cross browser request, you should use jsonp instead:
调用 $.getJSON() 后缺少一个分号。技术上不需要,但我发现如果没有它们,javascript 就会表现得很奇怪。当您使用浏览器点击 http://myserver:8000/get_yellow/ 时会发生什么?你看到 JSON 了吗?
在我在末尾添加 ']}' 后,您提供的示例 JSON 在 JSONLint 中进行验证(我假设这不是完整的响应。您确定它有效吗?来自 getJSON() 的 jQuery API:
There is a semicolon missing after your call to $.getJSON(). Not technically required but I've seen javascript act strange without them. What happens when you hit http://myserver:8000/get_yellow/ with your browser? Do you see the JSON?
The sample JSON you provided validates in JSONLint after I added a ']}' to the end (I'm assuming that isn't the full response. Are you sure that it's valid? From the jQuery API for getJSON():
从看来,我总是使用下面的代码将数据推送到javascript,并且效果很好。数据可以是 JSON 格式,也可以不是,请先尝试以下简单的情况,看看数据是否推送到 javascript:
如果您可以从 javascript 警告上述值,请在您的代码中尝试以下操作:
From the view, I always use the following code to push data to javascript and it works fine. The data can be in JSON format or not, try the following simple case first to see if data is pushed to javascript:
If you can alert the above values from javascript, try the following with your code: