Jquery ajax call for generate authorization code - javascript

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)

Related

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).

jQuery ajax post is sending request using get method on ipad chrome

When this code runs on chrome on ipad, it ignores the type "POST" and sends the ajax request using the get method. Is it a compatibility issue? Seems like chrome doesnt support post ajax requests?
$.ajax({
type: "POST",
url: "/ajaxCall",
data: sentData,
success: success,
dataType: "text",
error: function (jqXHR, textStatus, errorThrown) {
console.log(errorThrown);
}
});
I was testing the page on k-tunnnel/v-tunnel. I found out that the problem is becaouse of these sites. Without tunneling, my calls reaches the server properly.

"Uncaught SyntaxError: Unexpected token <" after successful HTTP Request

I am trying to do AJAX using jQuery with an ODATA REST service provided by an SAP backend system.
$.ajax({
type: 'GET',
url: 'http://sapxx.sapms/sap/opu/odata/.../',
async: true,
dataType: 'jsonp',
username: 'username',
password: 'password',
crossDomain: true,
success: function() {
alert('Done.');
},
error: function() {
alert('Error.');
},
});
The request returns status 200 and I get a response from the server which is called something like "?callback=jQuery3100687..." and contains xml code. All this is visible in the Chrome debugger. But after the successful HTTP Request I get the aforementioned error
Uncaught SyntaxError: Unexpected token <
I suspect the error is due to the "dataType" parameter which is set to "jsonp" in the request. Is there any way to work around this error? The server can only respond using XML format. The request only works when the dataType is set to "jsonp", I guess because it enables CORS. After sending the request, I get the "Error" alert despite the 200 status.
You say you get an xml response from the server. Your ajax request is set up for jsonp, though:
dataType: 'jsonp',
Try this instead:
dataType: 'xml',
success: function(xml) {
//remainder of the code
}
If you set the dataType to jsonp, jQuery will try to parse the response as JSON. This results in errors, because the response ins't JSON.

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/

Should ajax error reponseText come back as JS object

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

Categories

Resources