1

I need to get the json data from the cross domain.

$.getJSON('http://xx.xx.xx.xx/SampleService/Handler.ashx?callback=?', data, function (jsonData) {
                alert('1');
            })
            .done(function () { console.log("second success"); })
            .fail(function () { console.log("error"); })
            .always(function () { console.log("complete"); });

Handler code:

context.Response.ContentType = "application/json";
                SampleService service = new SampleService();

                List<List<Byte>> response = service.GetData();
                string jsonData = JsonConvert.SerializeObject(response);
                context.Response.Write(string.Format("{0}([{1}]);", context.Request["callback"], jsonData));

The error, I'm getting is:

"parsererror"
Error: jQuery19108131180874027861_1366004862133 was not called
benjamin54
  • 1,280
  • 4
  • 28
  • 48

2 Answers2

1

Use jsonp calls for Cross Domain requests. Use something like this

$.ajax({
        url : "http://xx.xx.xx.xx/SampleService/Handler.ashx",
        type: "GET",
        dataType: "jsonp",
        jsonp : "callback",
        success: function(data) {alert("Success");},
        error: function(data) { alert("Error"); }

        });        
   });

on your php page return result like this

echo $_GET['callback'] . "($result)";exit;

where $result is your json_encoded array.

chandresh_cool
  • 11,753
  • 3
  • 30
  • 45
0

The jQuery19108131180874027861_1366004862133 you are seeing is an auto generated callback wrapper function which jQuery attaches when you don't specify a callback function. ie you have callback=?. If you have access to the server side script which you are calling you can wrap your JSON in a function and call that using JSONP to get around the cross domain issue. Ie - name your callback function jsonCallback.

Server side script output:

jsonCallback(
    {
        [insert your json code here]
    }
);

Then client side:

(function($) { var url = 'http://www.jquery4u.com/scripts/jquery4u-sites.json?callback=?';

$.ajax({
   type: 'GET',
    url: url,
    async: false,
    jsonpCallback: 'jsonCallback',
    contentType: "application/json",
    dataType: 'jsonp',
    done: function(json) {
       console.dir(json);
    },
    fail: function(e) {
       console.log(e.message);
    }
});

})(jQuery);

Further reading: JQUERY’S JSONP EXPLAINED WITH EXAMPLES

Sam Deering
  • 369
  • 1
  • 7