Lakshmi
Lakshmi

Reputation: 53

AJAX: Invalid JSON response

Im using ajax to read my json data using

http:xxx.myserver.com/myfolder/example.json

$.ajax({
    url:"http:xxx.myserver.com/myfolder/example.json",
    dataType:"json",
    success:function(data, status) {
        alert("success"+data+":::"+status);
    },
    error:function(request, status, error) {
        alert("Error:"+JSON.stringify(request)+":::"+status+"::"+error);
    },
});

For this i get output like below

Error:{"readyState":0,"responseText":"","status":0,"statusText":"error"}:::error::

I googled for the solution some says to replace json to jsonp if i replace

dataType:json to dataType:jsonp

I got output like

Error{"readyState":4,"status":200,"statusText":"success"}:::parsererror::Error: jQuery110107852220840286463_1387290139272 was not called

It is not at all entering success function.

Please anyone provide me solution for this. Thanks in advance.

Upvotes: 1

Views: 3610

Answers (1)

Lakshmi
Lakshmi

Reputation: 53

I got solution for my problem.

Your json should be in this format Eg:jsonCallback({"sites":[{"siteName": "JQUERY4U","domainName": "http://www.jquery4u.com"},{"siteName": "BLOGOOLA","domainName": "http://www.blogoola.com"},{"siteName": "PHPSCRIPTS4U","domainName": "http://www.phpscripts4u.com"}]});

I changed my response to jsonCallBack

    $.ajax({
    url:"http:xxx.myserver.com/myfolder/example.json?callback=?",
    dataType:"jsonp",
    async: false,
    jsonpCallback: 'jsonCallback',
    contentType:"application/json",
    success:function(data, status) {
        alert("success"+JSON.stringify(data));
    },
    error:function(request, status, error) {
        alert("Error:"+JSON.stringify(request)+":::"+status+"::"+error);
    },
});

I get into success function and get my json response. Thanks for your help. This is because of cross domain problem.

Upvotes: 1

Related Questions