Ajax cross domain POST in IE8 and IE9 - javascript

I am trying to make a ajax cross domain POST in IE9 and IE8 to a rest endpoint that resets a password and am not having any luck. The ajax call I am trying to make is as follows:
$.ajax({
type: 'POST',
crossDomain: true,
url: postUrl,
contentType: 'application/json; charset=utf-8',
data:{ "userid":uid, "email":email, "password":password },
dataType: 'jsonp',
success: function (data) {
console.log(data);
},
error: function (status){
console.log(status);
}
});
I am also making sure to include the following before the POST call:
$.support.cors = true;
Everytime I make the call it goes into the error function. It says the status is success however when I try to login with the new password it is not working which means it wasnt a success. The following is the response that I output to the log:
[object Object]{readyState: 4, status: 200, statusText: "success"}
Also note that I am calling the jQuery.XDomainRequest.js by MoonScript. That several other stack overflow answers say to do. And my jquery version is 1.11.3
Can someone please provide me some direction on this.

Related

Jquery: Probably a syntax Issue in ajax() method - Value not getting sent

I'm able to dump value of the variable message in console .
But im not able to send it off in POST Request.
AJAX call:
chat.throwmsg = function(message) {
if ($.trim(message).length != 0) {
console.log(message);
$.ajax({
url: 'ajax/chat.php',
type: 'post',
data: { method: 'throw', message: message} ,
success: function(data) {
chat.fetchmsgs();
$('textarea#entry').val('');
}
});
}
}
This maybe due to wrong syntax, but I've tried both single and double quotes, and combination as well .
With a wild assumption, you are not having any error messages in developer console and chat.php has a post handler forthese parameters
Since your data is JSON, please change the code as this way and have a try..
var temp={ method: 'throw', message: message};
var param=JSON.stringify(temp);
$.ajax({
url: 'ajax/chat.php',
type: 'post',
data: param ,
dataType: "json",
success: function(data) {
chat.fetchmsgs();
$('textarea#entry').val('');
}
});
after reviewing the code I could not find any issues that restrict the data to be sent along with ajax request,if you are having any syntax errors you should have been warned prior the request initialization, however I recommend you to check the request header in the network tab of browser console and see your sending data along with the request if it's there you probably need to check the code of getting the post data in your server-side implementations

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

DreamFactory REST API POST rest/user/session request always returns error in IE9

The initial POST rest/user/session request works in Firefox, Chrome, and Safari, but when using Internet Explorer 9 it always returns some sort of error..
When the dataType is set to "json", IE9 gets back a 'no transport' error with a status of 0. When the dataType is set to "jsonp", IE9 gets back an error object with a success XHR status 200 with a parseError.
...
$.ajax({
url: BASE_PATH + url,
beforeSend: function (request) {
request.setRequestHeader("X-DREAMFACTORY-APPLICATION-NAME", APP_NAME);
request.setRequestHeader("X-DREAMFACTORY-SESSION-TOKEN", sessionStorage.SESSION_TOKEN);
request.setRequestHeader("Content-Type","application/json");
request.setRequestHeader("Accept","application/json");
},
method: method,
contentType: "application/json; charset=utf-8",
dataType: "jsonp",
processData : processData,
data: newdata,
cache: false,
success: function(response){
console.log(arguments);
$.jStorage.set("unique_session_id",response.session_id);
sessionStorage.SESSION_TOKEN = response.session_id;
runApp();
},
error: function(response){
console.log(arguments);
}
});
DreamFactory will only communicate successfully in IE10 or above. Implementation of REST, CSS, etc. was insufficient in IE9.
I recommend trying to navigate directly to the REST URI in IE9 to see if the response is returned properly. If so, it's an issue in your ajax script or implementation and not in DSP/IE9 specifically.
Some quick research also indicates to try this:
$.support.cors = true;

Token error when parsing return data in Ajax call

I am creating an app using HTML, jQuery Mobile, jQuery and Cordova. My requirement is to authenticate an user from the app by calling a remote server. I am developing my localhost WAMP stack.
My jQuery is as below.
$.ajax({
type: 'POST',
url: 'http://localhost/server/',
crossDomain: true,
data:{appkey:'1234567',action:'authenticate', username: u, password :p},
dataType: 'jsonp',
async: true,
success: function (data){
console.log(data);
},
error: function (jqXHR, textStatus, errorThrown) {
alert("Invalid Username or Password");
window.location = "index.html";
}
});
The remote call url is below. JQuery automatially adds ?callback= to the remote url as the datatype is jsonp, which is fine.
localhost/server/?callback=jQuery18306688079617451876_1383495199431…4567&action=authenticate&username=fdf&password=fdfdf&_=1383495208981
The response from the remote url is as below in Chrome Developer console.
{"success":"false"}
In the PHP part at server, I am using the below code to return the json object.
header('Content-type: application/json');
echo json_encode($araryVal);
It looks for me that all things are perfect, but my code breakpoint never reaches the success section of the ajax call. It always break out in error section.
In the developer console I see the error message.
Uncaught SyntaxError: Unexpected token :
JsonLint says the json is valid. Please let me know what I am doing silly.
I don't think your data type is JSONP, it's just JSON. If it were JSONP, your content-type should be "application/javascript" and your response text should wrap the JSON object it returns with the javascript function provided to the server in the callback parameter.
(Actually your error is probably PHP emitting a notice about an undefined variable: araryVal ... You sure that shouldn't be arrayVal? :)
I already accepted the answer provided by #Fabio. I just want to provide the technical details which solved my problem.
The hint from Fabio helped me to do some research on this. Thanks man.
I have added the callback options to the ajax call as below.
$.ajax({
type: 'POST',
url: 'http://localhost/server/',
crossDomain: true,
data:{appkey:'1234567',action:'authenticate', username: u, password :p},
dataType: 'jsonp',
contentType: "application/javascript",
jsonp: 'callback',
jsonpCallback: 'mycallback',
async: true,
success: function (data){
console.log(data);
},
error: function (jqXHR, textStatus, errorThrown) {
alert("Invalid Username or Password");
window.location = "index.html";
}
});
The remote call url is as below after the above change. JQuery now add "mycallback" instead of some random callback value "jQuery18306688079617451876_1383495199431…4567" for callback parameter.
localhost/server/?callback=mycallback&action=authenticate&username=fdf&password=fdfdf&_=1383495208981
In the PHP side I modified the output to include the callback value with the json string.
header('Content-type: application/javascript');
echo "mycallback(" . json_encode($araryVal) . ")";
Today was the day I understood how to use jsonp with callback. Thanks to all.

Categories

Resources