Should ajax error reponseText come back as JS object - javascript

I am sending a multipart form-data request w/ ajax:
$.ajax({
url: '/postit',
type: 'POST',
success: function(response) {
},
error: function(response) {
// response.responseText is json, but not parsed
},
data: someFormData,
cache: false,
contentType: false,
processData: false
});
I was hoping the error response would be parsed as it is JSON. Looking at the response headers from the request in chrome I see:
Content-Type:application/json; charset=utf-8
I have tried setting dataType: 'json' with no luck either. I suspect this is just a difference in how the error object is treated.
Is there a way to get the JQuery ajax call to automatically parse that error response into a JS object?

The error callback will give the following parameters:
error: function(jqXHR, textStatus, errorThrown) {
// ...
}
Where jqXHR is a superset of the native XMLHttpRequest which means that responseText is a DOMString.
While the success callback will give:
success: function(data, textStatus, jqXHR) {
// ...
}
Where data could be anything, parsed according to content type.
XMLHttpRequest.responseText Read only
Returns a DOMString that contains the response to the request as text, or null if the request was unsuccessful or has not yet been sent.
Source: https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest#Properties

Related

What's diffrence in jQuery ajax dataType="json" vs JSON.parse()

Is there any real diffrence between using dataType='json' and parse response by JSON.parse(response) in jQuery Ajax?
$.ajax({
url: path,
type: 'POST',
dataType: 'json',
data: {
block : lang,
},
}).done(function(response, textStatus, jqXHR)
{
output = response;
});
VS
$.ajax({
url: path,
type: 'POST',
data: {
block : lang,
},
}).done(function(response, textStatus, jqXHR)
{
output = JSON.parse(response);
});
dataType: 'json':
Sets the Accept request header to tell the server that JSON is the desired response format
Attempts to parse the response as JSON regardless of what the server's Content-Type response header says the format is. (jQuery's default behaviour is to use the Content-Type to determine the correct parser).
JSON.parse:
Attempts to parse the response data as a string of JSON regardless of what it actually is (if the server responds with correctly labelled JSON, this will error since it will have already been parsed by jQuery).

Jquery ajax call for generate authorization code

As part of my requirement, I would like to generate authorization code by calling O365 url from jquery ajax call. I am calling below script from document ready()
$.ajax({
type: 'GET',
dataType: 'jsonp',
async: false,
url: 'https://login.microsoftonline.com/{{tenant id}}/oauth2/authorize?client_id={{client id}}&response_type=code&redirect_uri=https://myurl.com/createpage/&response_mode=query&resource={{resource}}&state=9128',
crossDomain: true,
success: function(jsonData) {
alert(jsonData);
},
error: function(request, textStatus, errorThrown) {
console.log(errorThrown);
},
cache: false
});
But it is getting following error.
Jquery ajax error
Request you to help on resolving the issue.
You should check the content of the response in the tab network (in your dev tools).
I guess the response is not a valid json, so the parsing failed (your specified the jsonp type in your ajax call)

Web API Not Returning data using JQuery Ajax

I am newish to JQuery Ajax.
My code below always return the error function. Am I doing something wrong? I can see the json data in the response header using firebug I can't get the success function to work.
var url = "http://ec.europa.eu/budg/inforeuro/api/public/monthly-rates"
$.ajax({
crossOrigin: true,
type: "GET",
crossDomain: true,
async: false,
url: url,
dataType: 'jsonp',
jsonpCallback: 'jsonCallback',
contentType: "application/json",
success: function (data) {
console.log("Success");
},
error: function () {
console.log("Ajax Error Occurred");
}
});
Are you sure the source you're using the JSONP request on actually supports JSONP?
When calling the url http://ec.europa.eu/budg/inforeuro/api/public/monthly-rates?callback=foo (note the callback param) the response from the server does not contain a valid JSONP response (which would contain a call to the foo function).

Ajax jquery call to GET application/x-javascript

How do I get content of "application/x-javascript" using jquery Ajax call?
As it keep getting me null content.
What I am trying to use for now:
$.ajax({
dataType: "json",
contentType: "application/x-javascript;charset=utf-8",
url:the_url,
async:false,
success:function(r){
console.log("el result" + r) ;
response = r;
}
});
This:
dataType: "json",
tells jQuery to ignore what the server claims it is sending back and to process the result as JSON. JavaScript isn't JSON, so this breaks it.
Remove that line.
Then you should get the data in the success function.
Asides:
This:
contentType: "application/x-javascript;charset=utf-8",
claims you are sending JavaScript. You aren't making a POST request, so you aren't sending anything. Remove it.
Even if you were sending JavaScript to the server, the application/javascript MIME type hasn't been experimental since 2006, so it shouldn't have the x- prefix on it.
async:false, is a terrible idea. It locks up the JS event loop waiting for the response. You shouldn't use it.
response = r;: assigning data to globals is usually a terrible idea. Process the data in the success event handler instead.
Try this out :
$.ajax({
url: 'my/url',
type: 'GET',
data: 'test=mytest&test2=mytest2',
success: function (data) {
console.log(data);
},
error: function (jqXHR, textStatus, errorThrown) {
alert(errorThrown);
}
});
And read the documentation, to see what each parameter is made for :
http://api.jquery.com/jquery.ajax/

What happens when wrong dataType is returned

I perform a call
$.ajax({
type: "POST",
url: url,
data: dataToPost,
dataType: "json",
success: function(data, textStatus){ /*something*/ },
failure: myHttpReqErrorHandler
});
In certain cases when things goes wrong on the server I get
Content-type:text/html; charset=UTF-8
type, and content is a real HTML page, and there is not much I can do about that. I want to manage this case on JavaScript, but when this happens no callback is called on jQuery side (neither success nor failure).
Is there a further parameter to pass to ajax to handle that?
The error handler is error: function(){}
error: myHttpReqErrorHandler
It will throw a parse error(parsererror) if the content is not a parsable json format.
Demo: Fiddle

Categories

Resources