Back to Top

How to call Remote URL in jQuery Ajax?

To Call Remote URL in $.ajax method in Jquery

This time, I am going to explain about How to call Remote URL in jQuery Ajax?. Recently I am working on an android application and got a parse error when trying to execute remote url in ajax.

I am using remote URL to call web service which posts data to remote URL and returns a JSON response contains data and I have used jsonp for requesting the JSON data because AJAX doesn’t support cross domain calls by default because of security reasons.

JSONP is the light weight method to request data from a server to a different domain.JSONP is useful for cross domain issue and calls remote/external URL request.

Let’s view following code:

Problem & solution:

Here I have pass dataType as jsonp and important is to pass &callback= into url otherwise your code will not work.you will not get proper response.If &callback is missing, you will get parse error or error status = 0.

It’s a wrap!.This is the today’s quick tip! I’m hoping that you’ve found this technique useful.

Further Reading:

To use Ajax in WordPress
Solution: Internet connection issue on AndroidEmulator
To read JSON data with jQuery

As always, thanks for reading. Don’t Forget to Follow us on Twitter or Subscribe us to Get the Latest Updates.

Comments (4)

  1. Login to LuCI

    function loginme(){
    var uname = $(“#luci_username”).val();
    var password = $(“#luci_password”).val();
    //var w3school = “http://www.w3schools.com/jquery/ajax_ajax.asp”;
    //var statusURL = “http://192.168.1.18/cgi-bin/luci/;stok=ea9d3ffe240ce6ec6797d4980d51ade1?status=1”;
    var statusURL = “http://192.168.1.18”;
    $.ajax({ url: statusURL,
    type: “GET”,
    dataType: “jsonp”,
    contentType: “application/xml;charset=utf-8”,
    //accept-Charset: “ISO-8859-1,utf-8;q=0.7,*;q=0.3”,
    accept: “text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8”,
    success: function(result){
    alert (“I am in success”);
    },
    error: function(request, error){
    alert(“request status = “+request.status+ “,error : “+error);
    }
    });
    }

    I am getting a parsing error while running the scripts

    I am sometimes getting a html response and sometimes a json response.

    Though the response data is correct and validated using JSON decoder or a html decoder. My above code is giving a parsing error and it never going to success call back.

    1. Have you tried with callback in querystring?

  2. I am sometimes getting a html response and sometimes a json response.

    Though the response data is correct and validated using JSON decoder or a html decoder. My above code is giving a parsing error and it never going to success call back.

  3. Is this possible with this JSONP method?

    https://parselsorgu.tkgm.gov.tr/is a website that gives free area search on map with given coordinates. I am trying to implement this service in my jquery mobile app.

    Can i post coordinates from my app to that website? and get the response.

    The website is in Turkish, you know you can translate with chrome to english. You can test the service, with “Geographic Query” tab at main page and enter “40,0529” for Latitude and “26,2193” for Longitude at inputs.

    Note: In chrome developer console, you can view the get response as JSON (What we need). Than i need to draw polygon on google map component.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Most Popular Posts

Storing Objects in Html5 Local Storage

Posted on 7 years ago

Bhumi

PHP Tutorial for beginners

Posted on 7 years ago

Bhumi