1

I have the following json in one of the links:

{"edi":[{"NEdid":"19", "Publications":"B0001", "NCid":"141"
, "SEditionCode":"C0001", "SLang":"English", "STimeZone":"GMT+4:00"
, "SEdname":"Default", "NEdmon":"1", "NEdtue":"1", "NEdwed":"1"
, "NEdthu":"1", "NEdfri":"1", "NEdsat":"1", "NEdsun;":"1"
, "SFrequency":"Daily", "NSequence":"1", "dtCreatedOn":"2013-03-25 12:18:46.0"
, "NCreatedBy":"3", "dtModifiedOn":"2013-03-25 12:18:46.0"
, "NModifiedBy":"3", "BIsActive":"1", "BIsDeleted":"0", "NNoe":"7"}]}

I want to parse cross domain json. I am calling the following function on page load of a php page and using localhost. I am using the following code:

function loadEditionList(edurl) {

    if (window.XMLHttpRequest) {
        // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    } else {
        // code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4 && xmlhttp.status==200) {
            var jsonEdition = xmlhttp.responseText;
            var objEdition = JSON.parse(jsonEdition);

            for (var i = 0; i < objEdition.edi.length; i++) { 
                var editionname=objEdition.edi[i].SEdname;  
                alert(editionname);
            }
        }
    }
    xmlhttp.open("GET", edurl, true);
    xmlhttp.send();
}

Above code is working in IE with a alert message regarding security, but it is not working in chrome and mozilla. xmlhttp.status is 0 in chrome and mozilla.

Another code I used:

function loadEdition()
{
var getUrl = 'someurl/desktopReader.do?numPublisher=3&type=Editions&numPublication=19';

                $.ajax({
                    url : getUrl,
                    type : 'GET',
                    dataType : 'jsonp',
                    jsonp: 'jsonp',
                    crossDomain : true,
                    success: function() { alert('Success!'); },                                                                                                               
                    error: function() { alert('Uh Oh!'); },
                });           
}

In this case I always get alert 'Uh Oh!'.

Please, suggest me the proper way!

Mario Kutlev
  • 4,897
  • 7
  • 44
  • 62
Kandpal Lalit
  • 139
  • 1
  • 5
  • 4
    Telling jQuery that it is going to get JSONP won't help if the server doesn't *send* JSONP. – Quentin Apr 11 '13 at 10:30
  • 2
    possible duplicate of [Ways to circumvent the same-origin policy](http://stackoverflow.com/questions/3076414/ways-to-circumvent-the-same-origin-policy) – Quentin Apr 11 '13 at 10:30
  • Does that other-domain-service support CORS? Does that other-domain-service support [JSON*P*](http://en.wikipedia.org/wiki/JSONP)? If not, you have to use a proxy. – Bergi Apr 11 '13 at 10:33
  • Why the PHP tag ? ( I removed it) – nl-x Apr 11 '13 at 10:33
  • 2
    Note that the issue isn't really anything to do with parsing JSON, the issue is how to make a cross-domain request. How to parse the response is irrelevant when you aren't getting one. – nnnnnn Apr 11 '13 at 10:40
  • A edit my answer, check this please. – Saram Apr 11 '13 at 18:17

1 Answers1

0

Maybe problem is on server side. Here is example i give other guy: http://jsfiddle.net/YGm89/ The code:

 $.ajax({
   url: "http://ws.geonames.org/postalCodeSearchJSON",
   dataType: "json",
   data: {
      postalcode_startsWith: request.term
   },
   success: function(data) {alert("ok");}
});

Example with your code. I removed JSONP function callback name, becouse i can not see any in you request. I asume, thet you do standard JSON request (not JSON P).

Saram
  • 1,500
  • 1
  • 18
  • 35
  • It needs to be JSONP if it's cross-domain, unless you use CORS (which your example is). – gen_Eric Apr 11 '13 at 18:23
  • Thanks for pointing me on it. Maybe chagne it to CORS is the solution? Why to hange jsonp callback is also question for me: `jsonp: 'jsonp',` – Saram Apr 11 '13 at 20:15