2

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

Bud Damyanov
  • 30,171
  • 6
  • 44
  • 52
developer i'm
  • 29
  • 1
  • 2
  • 1
    You're not returning valid JSONP from your server. – adeneo Dec 09 '13 at 12:28
  • can you paste an example of the json you're getting back from the server? most chances there's something invalid going on there... – developer82 Dec 09 '13 at 12:30
  • 3
    Also, what the heck are you doing? You've added just about every option, and you're expecting to get JSONP back, but you've set the callback key name (the jsonp option) to false, turned of async, which isn't a gread idea when doing **Asynchronous** Javascript And Xml, and added your own callback function which basically removes the success handler, and when doing JSONP requests there is no error handler. – adeneo Dec 09 '13 at 12:34
  • ajaxSuccess or success?? – Joke_Sense10 Dec 09 '13 at 12:39
  • {'usage': [{ 'pagesVisits':'10' , 'updatesDone':'1' }]} this is the response from server @adeneo – developer i'm Dec 09 '13 at 12:39
  • i used both ajaxSuccess and success but no use. @Joke_Sense10 – developer i'm Dec 09 '13 at 12:40
  • {'usage': [{ 'pagesVisits':'10' , 'updatesDone':'1' }]} this is the response from server @developer82 – developer i'm Dec 09 '13 at 12:40
  • And that's not even close to being JSONP – adeneo Dec 09 '13 at 12:43
  • 1
    JSONP isn't really ajax, it's a script tag that is inserted in the DOM. What is important is that the returned code is valid JSON wrapped in a function, so JSONP will always look like `function_name( {json:here} )`, and your code has no wrapping function, hence it's not valid JSON, and you get a parse error – adeneo Dec 09 '13 at 12:49
  • As a sidenote, since JSONP isn't really ajax, there is no way to set async : false, or do POST requests, or .... you get my drift, it's alway an async GET request as it loads a javascript file – adeneo Dec 09 '13 at 12:51
  • is this code is in the same project as of c#, or you accessing it from outside ? – Murtaza Khursheed Hussain Dec 09 '13 at 12:58
  • i returned blah({'pagesVisits':'stackoverflow','updatesDone':'5'}); from my server but in vain. @adeneo – developer i'm Dec 09 '13 at 13:02
  • im using it from outside @MurtazaHussain – developer i'm Dec 09 '13 at 13:36
  • put it in the same project as of C# – Murtaza Khursheed Hussain Dec 09 '13 at 17:17

2 Answers2

1

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.

Community
  • 1
  • 1
Prateek
  • 6,785
  • 2
  • 24
  • 37
1

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

Stephen Ngethe
  • 1,034
  • 13
  • 24