Dojo xhrGet only returning null - javascript

The following Dojo code's load callback returns null. There shouldn't be any problem because jQuery's ajax works fine. What am I missing here?
Dojo version - doesn't work
dojo.xhrGet({
url:"http://localhost:11039/",
handleAs:"json",
load: function(data){
console.log(data); // Prints null
},
error: function(err){
console.log('Error: ' + err);
}
});
jQuery version - works
$.ajax({
url:"http://localhost:11039/",
type: 'GET',
dataType: 'json',
success: function(res){
console.log(res) // Prints some JSON
},
error: function(err){
console.log('ERROR: ' + err);
}
});
Looking at Firebugs net tab I notice that the jQuery version is in fact sending a GET request:
GET localhost:11039 200OK localhost:11039 62.8KB
while the Dojo version... "OPTIONS"?
OPTIONS localhost:11039 200OK localhost:11039 62.8KB
Additional Details:
I get the same result in Ffox, Chrome and Safari.

Dojo sending an OPTIONS request usually means dojo considers it a cross-domain request.
The OPTIONS request is checking for the Access-Control-Allow-Origin header to see if the request should be performed, even though it is considered cross-domain.
See https://developer.mozilla.org/En/HTTP_Access_Control for more details

Related

Can someone explain why I'm getting 500 Internal Error?

I'm trying to plot a graph using a flask. But I'm getting 500 Internal Error when I'm trying to run on the server. My web page is visible but when I'm trying to select a dropdown to plot graph I'm getting the error. My console.log(url) is getting printed but I guess the ajax function is not getting executed.
.
'function get_map(url, rs, matrix, isScree, chart_title) {
console.log(url);
$.ajax({
type: 'GET',
url: url,
contentType: 'application/json; charset=utf-8',
xhrFields: {
withCredentials: false
},
success: function(result) {
if(matrix) {
console.log("in get_map 1")
drawScatterPlotMatrix(result, rs, chart_title);
} else {
console.log("in get_map 2")
drawScatter(result, rs, chart_title);
}
if(isScree) {
console.log("in get_map")
draw_scree_plot(result, chart_title)
}
},
error: function(result) {
$("#error").html(result);
}
});
}'
It's likely that your Ajax request is being executed, as 500 is an HTTP error being returned by the server. If you watch the network tab in your browser's developer tools, you should see the request being made and the 500 being returned. Also, if you console log in the error portion of your Ajax request you should see the error that's being returned (in case your #error DOM element is not being rendered correctly).
So, the error is most likely something going wrong on the server. If your network response has no additional error information to help you, you might post a question with the server code handling the response. If it's a well established API, perhaps you could provide the URL.

JQuery ajax query fails very silently

I have read similar questions with similar problems but every advice I read seems to be inefficient for me. Here is the ajax call (I am using Jquery 1.9.1):
$( document ).ajaxError(function() {
alert( "AJAX ERROR" );
});
$.post( "/lists/complete", { itemID:id }, function(answer) {
alert("SUCCESS");
}, "text" ).fail( function() {
alert("ERROR");
}).always( function() {
alert("DONE");
});
On the server side the request is received as expected. If I monitor the server response on the client side (using Firebug), I can see that the server sends the 200 response along with the correct data in the body. However, no alert is never triggered !
What can I do to understand the issue ? I must add that I have very little experience with JS and with Jquery...
I'm also not a fan of jquery post. I prefer using $.ajax. However it is recommended to chain done, fail and always.
$.ajax({
url: ' yoururl ',
type: 'POST',
// dataType: 'default: Intelligent Guess (Other values: xml, json, script, or html)',
data: {param1: 'value1'},
})
.done(function() {
console.log("success");
})
.fail(function() {
console.log("error");
})
.always(function() {
console.log("complete");
});
http://api.jquery.com/jQuery.ajax/
Deprecation Notice: The jqXHR.success(), jqXHR.error(), and
jqXHR.complete() callbacks are deprecated as of jQuery 1.8. To prepare
your code for their eventual removal, use jqXHR.done(), jqXHR.fail(),
and jqXHR.always() instead.
I v never been a fan of the $.post i rather use the full ajax call :
$.ajax({
type: 'POST',
url: '/lists/complete',
data: data,
success: function(data){
alert("SUCCESS");
},
error: function(xhr, type, exception) {
// if ajax fails display error alert
alert("ajax error response type "+type);
}
});
Give this a shot and let me know if the alerts go off.
If this doesn't work out, open up the console (firebug...) and go to the network tab, clear it and send your request.
Click on the request and check the headers and response if they are normal.

Download JSON file via JavaScript/JQuery - HTTP-Status Ok - JQuery Error

I've got the following problem: I need to download a JSON file from an API via JQuery / JavaScript. In theory this should be quite basic.
I tried $.ajax and all of its siblings like $.get or $.getJSON. I alway get an 200 OK but my Firebug reports an error. Printing the error just says: "error" - so not that helful.
I read that maybe the JSON file is corrupt. So I tried it with a plain text file (*.txt). Same result.
The JSON file is valid, I check it against a validator.
I also tried ContentType and dateType and experimented with json and jsonp...
I basically used something like this (with a million variations for testing purposes):
$.ajax({
url: 'http://www.myurl.com/api/v1/myfile.json',
...
success: function(data) {
console.log(data);
},
error: function(error) {
console.log(error.statusText);
}
});
Am I missing something important here? It's really odd that nothing seems to change the behavior of the AJAX-call.
In fact I don't really need AJAX because I need to grab the JSON file when loading the page...
And the JSON file is not on the same domain as the AJAX caller.
Is that URL located on the same server you're trying to get the data from?
If not, you ran into a cross-domain request, which can only be handled using JSONP. So, the JSON file itself must be compatible with JSONP format, otherwise jQuery won't be able to process it (even if you provide a 'jsonp' dataType).
P.S.: Firebug will always show response code 200 but give an empty response body for such requests
Try in this way by disabling security
$.ajax( {
type : 'GET',
contentType : "application/json; charset=utf-8",
url : surl, \\specify your url
async : false,
dataType : 'json',
headers : {
Accept : "application/json",
"Access-Control-Allow-Origin" : "*"
},
crossDomain : true,
success : SucceedFunc,
error : function(data, textStatus, errorThrown) {
console.log("error" + ' ' + JSON.stringify(data) + ' ' + textStatus + ' ' + errorThrown);
}
});
function SucceedFunc(data) {
alert("success");
}
}
Did you try to catch the error the correct way?
$.ajax({
url: 'http://www.myurl.com/api/v1/myfile.json',
success: function(data) {
console.log(data);
},
error: function(error) {
console.log(error.message);
}
});
If you are using chrome go to cmd prompt and run the chrome by disabling the security. You can disable security using pathwhere_chrome_is_located\chrome.exe --disable-web-security
and run the html page. I think this may help you.

Jquery cross site Ajax call with 302 success data undefined with datatype script otherwise return error

I'm using JQuery 1.7.2, trying to do a cross site Ajax request which should return a html page via 4 redirects.
Not my ideal world with all those redirects, but it's part of the specification.
Now, using the following code:
$.ajax({
type: "GET",
url: myUrl,
dataType: "script",
success: function(data) {
alert("success :"+ data);
},
error: function(jqXHR, textStatus, errorThrown) {
alert("revoke: "+textStatus + ' / ' + errorThrown+"/"+jqXHR.status);
},
complete: function(jqXHR, textStatus){
alert("complete : "+jqXHR.statusText + ": "+jqXHR.readyState);
}
});
I can see in Firebug/Safari Developer Tools, that all the redirects work (e.g. returns a 302 status with a Location header).
Then the strange thing happens: At the last page, which returns a 200 status, my script ends and I try to view the data coming back. But the output is just "Undefined".
I agree on that I should not expect a script datatype when trying to get a html page, but when I tried with all the other datatypes (as defined in the jquery ajax page), the error handler is envoked and the status code is 0. All the while, in Safari DT, the status after the first redirect is just set as "(canceled)" (all the while the request just for the second redirect page just hangs in Firebug - but I'm just guessing that it has to do with their different implementation).
When I receive a 200 status, I can see in the debuggers that the last page has a size of some 18kb, which means that there should be some sort of data in it.
what to do?
You can't redirect with AJAX, you can only send and get data to and from the server.
For a redirection you have to use a "regular" HTTP request.
It can also be done with javascript window.location = myUrl

How to handle statuscode 307 redirect with jquery

I'm having a lot of trouble with the 307 Temporary Redirect statusCode using jquery.
I have an apache server that returns a 307 when needed. The 307 is getting thrown properly (I can see this with firebug or a wireshark trace), but I can't seem to get the redirect to trigger.
$.ajax({
url: someURL,
type: 'GET',
processData: false,
data: someData,
cache: true,
timeout: 5000,
statusCode: {
307: function() {
alert('307'); // this does NOT get called
}
},
error: function(request, status, error){
try {
console.log(request.getAllResponseHeaders());
}
catch (err) { }
},
success: function(data, textStatus, response){
try {
console.log(response.getAllResponseHeaders());
}
catch (err) { }
});
I wanted to check out the headers so I could redirect using the getAllResponseHeaders() method. This works fine for 200's but the 307 never triggers (in the success nor error handler)
One thing I noticed when using Firebug to examine the traffic is that right after the redirect an ajax GET is sent to the server with the url of the redirect! This results in a failure however.
So, what gives?
Edit:
I tried this with jquery 1.5.1 and 1.6.1
Well, I did some more research and it appears that a 307 is automatically handled by the browser, and is not able to be intercepted by jquery.
I ended up using a different statusCode to do the redirect (410 Gone).
This probably isn't the best solution, but I don't have access to the apache server code.

Categories

Resources