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.
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.
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.
None
0 Points
31 Posts
web api return no response and move to error function in ajax
Mar 23, 2020 09:19 AM|m_tack|LINK
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"e=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
All-Star
53021 Points
23607 Posts
Re: web api return no response and move to error function in ajax
Mar 23, 2020 01:25 PM|mgebhard|LINK
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.
All-Star
58174 Points
15650 Posts
Re: web api return no response and move to error function in ajax
Mar 23, 2020 02:47 PM|bruce (sqlwork.com)|LINK
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.