locked
web api return no response and move to error function in ajax RRS feed

  • Question

  • User-2103146910 posted

    Dear All,

    i want to fetch data from a cross domain rest api and using following code after getting help from internet surfing,

    <script type="text/javascript">
    function mycallback(json) {
    alert(json);
    }
    $(document).ready(function () {
    GetRates();
    function GetRates() {

    $.ajax({
    type: "GET",
    url: "https://www1.oanda.com/rates/api/v2/rates/spot.json?api_key=nQ1Skvoq86PzRqVbGkEkLVM3&base=USD&quote=EUR",
    dataType: "jsonp",
    crossDomain: true,
    cache: true,
    async: true,
    contentType: "application/json; charset=utf-8",
    jsonp: false,
    jsonpCallback: 'mycallback',
    xhrFields: {
    withCredentials: false
    },
    headers : {
    'Access-Control-Allow-Origin':'*'
    }
    }).done(function (data) {
    alert("success");
    }).fail(function (jqXHR, textStatus, errorThrown) {
    alert("error");
    });

    }
    });
    </script>

    i verify it in inspect element as follow:

    the problem is when i execute the code, i got response status code 200, as shown below:

    Status Code:200

    and in response tab i got following:

    No response data available for this request

    so eventually after ajax call i got response in error (fail) function instead of success (done) function. secondly when i enter the url in browser it gives me my desired response.

    please tell me what i am missing. i have already wasted my two days in it. need reply urgently.

    will be thankfull

    Monday, March 23, 2020 9:19 AM

All replies

  • User475983607 posted

    You configured the AJAX function for JSONP but the URL returns JSON not a script tag.  Secondly, you posted this question in a Web API forum but you code is browser based.  Anyway, I created a working example using an MVC View.

    @{
        ViewData["Title"] = "Home Page";
    }
    
    <div>
        <input id="Button1" type="button" value="button" />
    </div>
    
    @section scripts {
        <script>
                
            $('#Button1').click(function () {
                $.ajax({
                    type: "GET",
                    url: "https://www1.oanda.com/rates/api/v2/rates/spot.json?api_key=nQ1Skvoq86PzRqVbGkEkLVM3&base=USD&quote=EUR",
                    dataType: "json",
                }).done(function (data) {
                    console.log(data);
                }).fail(function (jqXHR, textStatus, errorThrown) {
                    console.log(errorThrown);
                });
            });
            
        </script>
    }

    Monday, March 23, 2020 1:25 PM
  • User-474980206 posted

    jsonp works by generating a script tag with the source set to the url. the server is supposed to detect its a jsonp call and render script with a callback. jQuery set the callback, name in the url, and this callback is used to call success.  with jsonp there is no error detection, as there is no real ajax call, just the script render and the rendered script making the callback. also the server must support jsonp calls. as there is no better cross site scraping support, jsonp support has gone out of style.

    Monday, March 23, 2020 2:47 PM