developer i'm
developer i'm

Reputation: 29

jQuery Ajax callback was not called. ParserError

I've a function that calls jquery ajax to hit a restful service developed in C#. the code is as follow

function refreshUsage(macAdd)
{
    alert(macAdd);
    ajax = $.ajax({ 
       type: "GET",    
       data: {'mac': '1'},
       dataType: "jsonp",
       jsonp:false,
       async:false,
       jsonpCallback:'blah',
       crossDomain: true,
       cache: false,
       contentType: "application/json; charset=utf-8",
       url: "http://localhost:20809/api/keyGen",      
       ajaxSuccess: function(data)
       {
        data = JSON.parse(data);
        console.log("data _" +data);
       },
       error: function(xhr, status, error) {
        if (status === 'parsererror') {
                console.log("resptext__" + xhr.responseText)
            }
          console.log("status _" +status);
          console.log("error _" +error);
       },     
       complete: function(response)
       {
        console.log("response _" +response);
       },

    });  
}
var blah = function (data) {
    alert(data);
    //do some stuff
}

when i hit this given url, it's sending response in browser window. but when im trying to get the response text in ajax function it's coming undefined even though success code is 200 and success text is success. I'm getting following errors and no response:

resptext__undefined
status _parsererror
error _Error: blah was not called

Upvotes: 2

Views: 9682

Answers (2)

Stephen Ngethe
Stephen Ngethe

Reputation: 1044

for me I had to ensure that the server is responding as expected by the jsonp callback I had to edit the response from the my php server as below

    $data = array ('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5); 
    echo $_GET['callback'] . '('.json_encode($data).')';

if your server does not reply in the format like

             dataWeGotViaJsonp({"statusCode":201}) 

you will be getting that error

hope this helps

Upvotes: 1

Prateek
Prateek

Reputation: 6975

This is example. Try this:

<html>
        <head>
            <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
            <script>
                $(document).ready(function(){
                    $.ajax({
                        url: 'http://twitter.com/status/user_timeline/padraicb.json?count=10',
                        dataType: 'jsonp',
                        success: function(dataWeGotViaJsonp){
                            var text = '';
                            var len = dataWeGotViaJsonp.length;
                            for(var i=0;i<len;i++){
                                twitterEntry = dataWeGotViaJsonp[i];
                                text += '<p><img src = "' + twitterEntry.user.profile_image_url_https +'"/>' + twitterEntry['text'] + '</p>'
                            }
                            $('#twitterFeed').html(text);
                        }
                    });
                })
            </script>
        </head>
        <body>
            <div id = 'twitterFeed'></div>
        </body>
    </html>

Example of ThatGuy will explain you.

Upvotes: 1

Related Questions