简体   繁体   中英

Cross-domain request through $.ajax

Consider the follwoing code :

$("#btn1").click( function() {      

     $.ajax({ 
       type: "GET",
       dataType:       'jsonp',
       jsonpCallback: 'jsonCallback',  
       url: myurl,

        success: getObj,

        error: function (xhr, status, errorThrown) {

                    alert(xhr.statusText);

            alert(xhr.responseText);

            alert(xhr.status);

            alert(errorThrown);

                }           

    });

and the success function is

function getObj(data) {




    $.each( data, function( key, value ) {

      alert( key + ": " + value );

        });
});

When i try the to access the url directly it returns the data like this:

[{"id":405,"parentId":404,"createdBy":"iipaxAdm","displayName":"A-2013-04-1""eventCount":"0"},{"id":601,"parentId":404,"createdBy":"iipaxAdm","displayName":"A-2013-04-2","eventCount":"0"},{"id":701,"parentId":404,"createdBy":"iipaxAdm","displayName":"A-2013-04-3","eventCount":"3"}]

But when traverse the data(return result) it only give the first result :

{"id":405,"parentId":404,"createdBy":"iipaxAdm","displayName":"A-2013-04-1""eventCount":"0"},

When checked in the developer tool the response contains the all results.

How can make sure that data should contain all reslutls?

Might be that the javascript thinks it is a string and not an object. Since it is JSON, you can use JSON.parse(data) to get the data as an object or array of objects, then traverse it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM