2

I try to get JSON data from my web server but it doesn't work.

$.mobile.showPageLoadingMsg();
//$.getJSON('http://localhost:8000/'+site+'/?format=json&callback=?', {}, function(){alert();});

$.ajax({
    url : 'http://localhost:8000/'+site +'/?format=json&callback=?',
    type : 'GET',
    dataType : 'jsonp',
    jsonp : 'callback',
    success : function(){alert('success');},
    error : function(){alert('fail')}
});

$.getJSON, $.ajax two method's callback function didn't firing at all. What's the problem?

My web server's code:

response_data.append({
            'user_nickname' : post.user_nickname,
            'title' : post.title,
            'recommend' : post.recommend,
            'oppose' : post.oppose,
            'date' : str(post.date),
            'hit' : post.hit,
            'commentcount' : post.commentcount
            })
    return HttpResponse(simplejson.dumps(response_data), mimetype='application/json')

In the insepector, it returns get, 200, ok, so HttpResponse has no problem.

Here is response:

[{"hit": 5, "title": " \uc624\ud1a0\uc774\uc2a4\ucf00\uc774\ud551 \ub418\ub294\uac00", "commentcount": 0, "oppose": 0, "recommend": 0, "date": "2012-07-24 07:01:22.453000+00:00", "user_nickname": "\ud55c\uae00\ub85c\uc5bc\ub9c8\ub098\uae38\uac8c\uae4c\uc9c0\uac00\ub2a5\ud558\ub7ef\uc778\u3131\u3147\u3139\u3147\ub05d"}, {"hit": 4, "title": "\uc5ec\uae30 \uae00\uc4f0\uba74?", "commentcount": 1, "oppose": 0, "recommend": 0, "date": "2012-07-24 06:52:05.125000+00:00", "user_nickname": "\ud55c\uae00\ub85c\uc5bc\ub9c8\ub098\uae38\uac8c\uae4c\uc9c0\uac00\ub2a5\ud558\ub7ef\uc778\u3131\u3147\u3139\u3147\ub05d"}]

$.ajax() never fires the success callback function. it just calls error's alert('fail');

1
  • do you have a javascript function callback? (as in function callback() { alert('hello'); } )? Commented Jul 28, 2012 at 9:07

1 Answer 1

2

Your response isn't JSONP: it's just a JSON array.

If you change the datatype in your ajax call to 'json', then your callback should fire.

But if you really need to use JSONP - if you're trying to do cross-site communication - then you'll need to return valid JSONP as your server response. Your server will need to respond with a string in the form of a javascript function call, using the value of the jsonp parameter as the function name, and the JSON response as the function argument.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.