简体   繁体   中英

jQuery $.getJSON not working

I am try to get a URL from a one server and using that URL to get contents of another server.

$.ajax({url : 'http://localhost:8080/geturl.jsp?A=1&B=2,C=3',
    success : function (data)
        {
            alert(data);
            $.getJSON(data, function (mydata)
            {
                alert(mydata);
            });
        },
    error : function (data, status, xhr)
            {
            }
    });

I know that we cannot make cross-domain requests in through ajax call, thats why i am using getJSON, i have the following problems

  1. When i simply pass the data to the url part of getJSON (as shown in the code), the alert-box show the correct URL but no get request is being performed ( get requests were monitored from FireBug).
  2. When a hard-code the data to be "http://www.google.com" then the get request is being performed but the no response comes, although the response headers comes and response code is 200 (but it was marked as RED in the Firebug (Dont know why:( )
  3. When I tries to fetch a webpage host in localhost domain, then it is fetched correctly although the response was not JSON.

I have the following doubts

  1. If the getJSON function accecpts only JSON objects as reponse then why no error came when perform above 3.
  2. Whats the correct code to perform my the required functionality.
  3. Suggestions to what happened in each case

Thanks in advance for the answers:)

The getJSON function can only be used across domains to fetch JSONP .
It does not magically evade any security restrictions.

http://api.jquery.com/jQuery.ajax/

This should be a working example for jsonp:

    var request = jQuery.ajax(
    {
        url: "http://Your url",
        success: function (data) { console.log('success!'); console.log(data); },
        error: function (data) { console.log('error!'); console.log(data); },
        dataType: "jsonp",
        type: "GET",
        data: { key: 'value' }
    });

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