Current application has to retrieve information from another application, and this other application is not required to be active to respond the JSONP request so the initiate requester will pop up an alert message about it.
function jsonRequest(requestURL, errorMsg){
var err = "";
var requestData= {param1: value1, param2: value2};
$.ajax({
type: 'GET',
async: true,
data: requestData,
jsonpCallback: 'jsonCb',
url: requestURL,
timeout: 20000,
dataType: 'jsonp', /* this trigger the syntax error window by IE*/
contentType: "application/json; charset=utf-8",
success: function(data) {
if(data.hasError != null){
error = data.error;
alert(error);
}else{
//.... logic to output valid values
} // closing } is not missing..duh
},//success
error:function(x, timeout, m) {
alert(errorMsg);
}
});
return err;
}
so then there are three possible scenarios:
JSONP request receives valid data from the other application
JSONP request receives empty data from the other application
JSONP request gets no response(the other application is not active)
So far so good until testing on IE. The problem is when it comes to scenario 3 then IE pop up its classic Syntax Error, after click 'close' then the alert message in $.ajax error:{..} shows up
Message: Syntax error
Line: 1
Char: 1
Code: 0
URI:.......
IE debug tool is pretty lame so it wont allow me to go any details. After I check javascript/jsp code line by line I found the cause of the issue:
In Scenario 3, once I change dataType: "jsonp" to dataType: "json" in the javascript code, then the error no more pop up, but of course the whole ajax request gonna fail. I cannot find out what returns to IE by the debugging tool, when the other application is inactive.
I wonder if there is any effective way to let IE to tolerate JSONP or any method to debug where is the cause of the issue.
The fact that IE9 works in any scenario with your code is a testament to the sheer incompetence of the microsoft programmers that created the javascript engine for that dinosaur
/rant - Solution to your problem follows: look for // you forgot this closing brace
function activateManifestJson(aUrl, code, timeoutErr){
var error = "";
// RESTful request data
var urlData = {param1: value1, param2: value2};
$.ajax({
type: 'GET',
url: aUrl,
async: true,
data: urlData,
jsonpCallback: 'jsoncallback',
timeout: 20000,
dataType: 'jsonp', /* this trigger the syntax error window by IE*/
contentType: "application/json; charset=utf-8",
success: function(json) {
if(json.hasError != null && json.hasError == "true"){
error = json.error;
alert(error);
}else{
//.... logic to output valid values
// *******************************************
} // you forgot this closing brace
// ***********************************************
},//success
error:function(x, tm, m) {
alert(timeoutErr);
}
});
return error;
}
After several hours desperately debugging, finally the fix to this issue emerged:
Just put this setting in ajax code and then the script error never pop up:
crossDomain: true,
so that
$.ajax({
type: 'GET',
url: aUrl,
async: true,
data: urlData,
jsonpCallback: 'jsoncallback',
crossDomain: true, /***** the life saver ****/
timeout: 20000,
dataType: 'jsonp',
contentType: "application/json; charset=utf-8",
success: function(json) {
if(json.hasError != null && json.hasError == "true"){
error = json.error;
alert(error);
}else{
//.... logic to output valid values
}
},//success
error:function(x, tm, m) {
alert(timeoutErr);
}
});
works just fine
Related
Return error in the query
From the browser the answer is correct.
$.ajax({
type: "POST",
url: url,
async: true,
contentType: " charset=utf-8",
dataType: "XMLHttpRequest",
success: function (response) {
console.log(response);
},
error: function (msg) {
console.log(msg);
}
});
The message says "error".
I see three issues. First, dataType is a choice of xml, json, script, or html, unless you did something really fancy. jQuery can guess it based on received data though, so there is normally no need to set it. But if you want to be explicit (assuming your page returns json):
dataType: "json"
Second, contentType value looks like some truncated thing. I would just completely remove it, as you are not sending any data and just requesting a page.
Finally, when you are sending no data and just requesting a resource, the best is to use GET.
All in all:
$.ajax({
type: "GET",
url: url,
async: true,
dataType: "html",
success: function (response) {
console.log(response);
},
error: function (msg) {
console.log(msg);
}
});
This is my simple code to call asmx webservice (xml).
function maxTransaccion() {
$.ajax({
type: "POST",
url: "WebService.asmx/MAxTransaccion",
contentType: "application/json; charset=utf-8",
dataType: "json",
crossDomain: true,
success: function(s) {
return s.d;
}
});
}
But I received this error:
message: "s is not defined"
proto: Error
I am doing something wrong? I use this ajax structure multiple times within a .js file. But only in this function it gives me error, what scares me is that it is so simple
First of all, if your service responds with a XML, then you should adapt for that:
$.ajax({
type: "POST",
url: "WebService.asmx/MAxTransaccion",
dataType: "xml",
crossDomain: true,
success: function(s) {
return s.d;
}
});
I think changing dataType and omitting contentType might do the trick.
The next thing that could be improved is your success-handler.
Check for the property first, before using it:
function(s) {
if (s && s['d']) {
doSomethingWith(s.d);
}
}
But because you are most likely receiving a XML and not a JSON-object, you might want something like this:
function(xml) {
var responseNode = $(xml).find('node');
doSomethingWith(responseNode.text());
}
Also like mentioned in the comments, just returning in an AJAX-call, will probably do nothing. So you need another function, where you get your result and doSomethingWithIt.
While using IE and posting to AWS getting the below error.
Here is my ajax request
$.ajax({
type: 'POST',
url: postingurl, //s3 bucket url
data: fd,
processData: false,
// contentType: false, // This not worked
// contentType: "multipart/form-data", // This also not worked
error: function(xhr, ajaxOptions, thrownError) {
alert(xhr.responseText); // error is the title of this question
}
}).done(function(data) {
// Success
});
Tried to remove the content type also. This working fine in Mozilla and Chrome.
Not working in IE. I am using IE 10. any pointers ?
I read that IE10+ is able to do cross-domain normally like all the other browsers, but it does not seem to work in my case.
When I start the request, IE simply reloads the page.
ajaxreq = $.ajax({
postData = {"movie":movie, "season":season};
url: "/cgi-bin/find_data.py",
type: "post",
datatype:"json",
cache: false,
async : true,
data: postData,
success: function(response){
var json = $.parseJSON(response);
}
})
.fail(function(err) {
alert("error" + err);
});
The same happens with XDR, though I would need that only if I cared about IE<10.
There are two solutions:
Change the input type of the button from submit to button
Add return false; in the onclick event
I'm calling a page (external, on other domain), with this code:
var instagram_container = $('div#instagram-answer');
if (instagram_container.length>0)
{
var url = 'http://www.xxxx.it/admin/get_instagram_token';
$.ajax({
type : 'GET',
url : url,
async : false,
jsonpCallback: 'jsonCallback',
contentType: 'application/json',
dataType: 'jsonp',
success: function(response)
{
console.log(response);
instagram_container.html(response.client_id);
},
error: function(e)
{
console.log(e);
}
});
}
I have answer with console.log(e) (basically it recognizes as error)?
Object { readyState=4, status=200, statusText="success", altri elementi...}
But in Firebug, under NET tab, I have the right answer...
This is the console. Line 19 is exactly the
console.log(e);
in error section.
My goal is obtain that Json. Thank you
Is your server returning plain text, or an actual JSON file? Jquery will ignore the response if the server returned a string instead of JSON.
The response type should be: "application/json"
EDIT:
I'd recommend you to use the basic jquery ajax call:
$.ajax({
url: "test.html",
cache: false
})
.done(function( response ) {
console.log(response);
});