开发者

jQuery $.getJSON not working

开发者 https://www.devze.com 2023-03-10 13:24 出处:网络
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\',

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, 开发者_Python百科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' }
    });
0

精彩评论

暂无评论...
验证码 换一张
取 消