jQuery Cross Domain Ajax

前端 未结 7 1926
Happy的楠姐
Happy的楠姐 2020-12-09 05:06

My ajax code is

$.ajax({
    type: \'GET\',
    dataType: \"jsonp\",
    processData: false,
    crossDomain: true,
    jsonp: false,
    url: \"http://someo         


        
7条回答
  •  情深已故
    2020-12-09 05:23

    You just have to parse the string using JSON.parse like this :

    var json_result = {"AuthenticateUserResult":"{\"PKPersonId\":1234,\"Salutation\":null,\"FirstName\":\"Miqdad\",\"LastName\":\"Kumar\",\"Designation\":null,\"Profile\":\"\",\"PhotoPath\":\"\/UploadFiles\/\"}"};
    
    var parsed = JSON.parse(json_result.AuthenticateUserResult);
    console.log(parsed);
    

    Here you will have something like this :

    Designation
    null
    
    FirstName
    "Miqdad"
    
    LastName
    "Kumar"
    
    PKPersonId
    1234
    
    PhotoPath
    "/UploadFiles/"
    
    Profile
    ""
    
    Salutation
    null
    

    And for the request, don't forget to set dataType:'jsonp' and to add a file in the root directory of your site called crossdomain.xml and containing :

    
    
    
    
    
    
    
    
    
    
    
    

    EDIT to take care of Sanjay Kumar POST

    So you can set the callback function to be called in the JSONP using jsonpCallback!

    $.Ajax({
        jsonpCallback : 'your_function_name',
        //OR with anonymous function
        jsonpCallback : function(data) {
            //do stuff
        },
        ...
    });
    

提交回复
热议问题