Make cross-domain ajax JSONP request with jQuery

前端 未结 7 1947
鱼传尺愫
鱼传尺愫 2020-11-22 04:10

I would like to parse JSON array data with jquery ajax with the following code:



        
相关标签:
7条回答
  • 2020-11-22 04:38

    You need to use the ajax-cross-origin plugin: http://www.ajax-cross-origin.com/

    Just add the option crossOrigin: true

    $.ajax({
        crossOrigin: true,
        url: url,
        success: function(data) {
            console.log(data);
        }
    });
    
    0 讨论(0)
  • 2020-11-22 04:38

    you need to parse your xml with jquery json parse...i.e

      var parsed_json = $.parseJSON(xml);
    
    0 讨论(0)
  • 2020-11-22 04:42

    Your JSON-data contains the property Data, but you're accessing data. It's case sensitive

    function jsonparser1() {
        $.ajax({
            type: "GET",
            url: "http://10.211.2.219:8080/SampleWebService/sample.do",
            dataType: "json",
            success: function (xml) {
                alert(xml.Data[0].City);
                result = xml.Code;
                document.myform.result1.value = result;
            },
        });
    }        
    

    EDIT Also City and Code is in the wrong case. (Thanks @Christopher Kenney)

    EDIT2 It should also be json, and not jsonp (at least in this case)

    UPDATE According to your latest comment, you should read this answer: https://stackoverflow.com/a/11736771/325836 by Abdul Munim

    0 讨论(0)
  • 2020-11-22 04:45

    alert(xml.data[0].city);

    use xml.data["Data"][0].city instead

    0 讨论(0)
  • 2020-11-22 04:49

    Concept explained

    Are you trying do a cross-domain AJAX call? Meaning, your service is not hosted in your same web application path? Your web-service must support method injection in order to do JSONP.

    Your code seems fine and it should work if your web services and your web application hosted in the same domain.

    When you do a $.ajax with dataType: 'jsonp' meaning that jQuery is actually adding a new parameter to the query URL.

    For instance, if your URL is http://10.211.2.219:8080/SampleWebService/sample.do then jQuery will add ?callback={some_random_dynamically_generated_method}.

    This method is more kind of a proxy actually attached in window object. This is nothing specific but does look something like this:

    window.some_random_dynamically_generated_method = function(actualJsonpData) {
        //here actually has reference to the success function mentioned with $.ajax
        //so it just calls the success method like this: 
        successCallback(actualJsonData);
    }
    

    Summary

    Your client code seems just fine. However, you have to modify your server-code to wrap your JSON data with a function name that passed with query string. i.e.

    If you have reqested with query string

    ?callback=my_callback_method
    

    then, your server must response data wrapped like this:

    my_callback_method({your json serialized data});
    
    0 讨论(0)
  • 2020-11-22 04:55

    use open public proxy YQL, hosted by Yahoo. Handles XML and HTML

    https://gist.github.com/rickdog/d66a03d1e1e5959aa9b68869807791d5

    0 讨论(0)
提交回复
热议问题