django - 对 jQuery 的 JSON 响应

发布于 2024-10-24 20:16:58 字数 1412 浏览 4 评论 0原文

我试图将这两部分粘合在一起,我什至不知道问题出在哪里,没有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 技术交流群。

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

发布评论

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

评论(3

最后的乘客 2024-10-31 20:16:58

您的 JavaScript 中存在语法错误。你少了一个分号。应该是:

$(document).ready(function(){
    $.getJSON('http://myserver:8000/get_yellow/', 
    function(data) {
        alert('Fetched ' + data.length + ' items!');
    });
});

另外,如果您要寻找的只是调试 api 代码的方法,只需输入 url (http: //myserver:8000/get_yellow/)进入当前浏览器的地址栏。在尝试使用 javascript 调用它之前,您可能应该首先执行此操作。

更新:

如果这不是跨浏览器请求,您的调用应如下所示:

$(document).ready(function(){
    $.getJSON('get_yellow/', 
    function(data) {
        alert('Fetched ' + data.length + ' items!');
    });
});

如果是跨浏览器请求,则应使用 jsonp 代替:

$(document).ready(function(){
    $.ajax({
       type: "GET",
       url: "http://myserver:8000/get_yellow/",
       dataType: "jsonp",
       success: function(data) {
           alert('Fetched ' + data.length + ' items!');
       }
     });
});

There's a syntax error in your javascript. You're missing a semicolon. Should be:

$(document).ready(function(){
    $.getJSON('http://myserver:8000/get_yellow/', 
    function(data) {
        alert('Fetched ' + data.length + ' items!');
    });
});

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:

$(document).ready(function(){
    $.getJSON('get_yellow/', 
    function(data) {
        alert('Fetched ' + data.length + ' items!');
    });
});

If it is a cross browser request, you should use jsonp instead:

$(document).ready(function(){
    $.ajax({
       type: "GET",
       url: "http://myserver:8000/get_yellow/",
       dataType: "jsonp",
       success: function(data) {
           alert('Fetched ' + data.length + ' items!');
       }
     });
});
渡你暖光 2024-10-31 20:16:58

调用 $.getJSON() 后缺少一个分号。技术上不需要,但我发现如果没有它们,javascript 就会表现得很奇怪。当您使用浏览器点击 http://myserver:8000/get_yellow/ 时会发生什么?你看到 JSON 了吗?

在我在末尾添加 ']}' 后,您提供的示例 JSON 在 JSONLint 中进行验证(我假设这不是完整的响应。您确定它有效吗?来自 getJSON() 的 jQuery API:

从 jQuery 1.4 开始,如果 JSON 文件
包含语法错误,请求
通常会默默地失败

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

As of jQuery 1.4, if the JSON file
contains a syntax error, the request
will usually fail silently

一城柳絮吹成雪 2024-10-31 20:16:58

从看来,我总是使用下面的代码将数据推送到javascript,并且效果很好。数据可以是 JSON 格式,也可以不是,请先尝试以下简单的情况,看看数据是否推送到 javascript:

from django.utils import simplejson

def get_yellow(request):
    colours = ['red', 'blue', 'yellow']
    data = simplejson.dumps(colours)
    return HttpResponse(data, mimetype='application/javascript')

如果您可以从 javascript 警告上述值,请在您的代码中尝试以下操作:

def get_yellow(request):
   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')
   return HttpResponse(simplejson.dumps(data), mimetype='application/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:

from django.utils import simplejson

def get_yellow(request):
    colours = ['red', 'blue', 'yellow']
    data = simplejson.dumps(colours)
    return HttpResponse(data, mimetype='application/javascript')

If you can alert the above values from javascript, try the following with your code:

def get_yellow(request):
   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')
   return HttpResponse(simplejson.dumps(data), mimetype='application/javascript')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文