Nested AJAX Functions - 500 Internal Error - javascript

I'm trying to run a script and then if there's success, I want to run another script. I'm doing this through jQuery and AJAX. I've tried both scripts individually and they both work by themselves (the AJAX functions). Here is the code:
$.ajax({
url: '/v/vspfiles/inventory-update/automation.asp',
success:function(data){
if(data=="True"){
$.ajax({
type: 'GET',
data:"filename=inventory.csv",
url: '/v/vspfiles/inventory-update/createxml.asp',
success:function(data){
alert('it worked');
}
});
}
}
});
I get a 500 internal error on the /v/vspfiles/inventory-update/createxml.asp?filename=inventory.csv when I run the code this way. I don't know why since the code works when I visit the page directly and when I run the AJAX by itself but when it's nested I get a 500 error.
Any idea why that would be happening? Thanks for your help!

There's nothing wrong with your code; nesting of ajax calls is allowed. Your server may be configured improperly. You can try to delay the second ajax call to see if that make any difference.
$.ajax({
url: '/v/vspfiles/inventory-update/automation.asp',
success:function(data){
if(data=="True"){
setTimeout( function() {
$.ajax({
type: 'GET',
data:"filename=inventory.csv",
url: '/v/vspfiles/inventory-update/createxml.asp',
success:function(data2){
alert('it worked');
}
});
},5000 );
}
}
});
5 seconds may be too long but it should prove to you that nesting ajax calls is quite proper and may point to what configuration changes may be required on the server. Give it a try. After all, that's what troubleshooting is all about.

Related

AJAX result OK but no data

I'm using jQuery to perform following AJAX request:
jQuery.ajax({
url: "url-to-script",
dataType: "html",
async: false,
success: function(data) {
console.log(data);
},
error:function(){
showMessage('Chyba', 'error');
}
});
Script that I'm calling is returning part of HTML. It is working properly.
But when I call this AJAX it will end with blank response in data. When I call it again it will return what it should and each next call is working. But the first one after page load is never working. I've no idea why.
I've also tried
if (data)
// do something
else
try again
But it just freeze my browser. So I don't know what is wrong.
Thanks for ideas!

jQuery AJAX request getting response but no callbacks are fired

I have code as such:
$.ajax({
method: "GET",
url: page,
dataType: 'html',
data:data,
success: function(data){
console.log(data);
},
error: function(){
console.log('error');
}
});
Using either Chrome or Firefox's debugger I can see that the request is successful with a response of 200 OK and the response contains the expected data.
My problem is, however, that no callbacks fire whatsoever. "Success" does not fire nor does "Error". Furthermore, no deferred methods fire either, such as "done", "then", or "always".
I've been trying to trouble shoot this for the past few hours to no avail. I'm at a total loss. I've also tried using methods such as "$.get" with the same result.
In short, I'm getting the response, but the code in jQuery is not firing any callbacks and all without any visible errors in the console.
One thing I see wrong in your code is that:
method: "GET",
should be:
type: "GET",
I don't see any documented property for method in the jQuery doc. The type property is supposed to default to "GET" so this may not be the only thing wrong here.
In addition, there are cases where the error callback will not be called even if the ajax call fails (in cross-domain requests). From the jQuery doc for the error callback:
This handler is not called for cross-domain script and cross-domain JSONP requests.
This is because jQuery is expecting the server to send back a particular form of javascript and if the server doesn't do what is expected, then jQuery never knows when the request comes back and can't process it.
In these cases, you often have to figure out what might be going wrong from looking at the network trace in the debugger.
Other things to check to make sure you aren't accidentally cross domain:
Make sure the domain/subdomain are exactly the same between ajax call and the page. For example, one common mistake is for one to have www. on it and the other not.
Make both page and ajax URL are both same http or https.
If using a non-standard port number, make sure both page and ajax URL are using the same port.
The following code works. Also note that AJAX will not work with cross site scripting.
If you want to get the error you can print the "errorThrown"
<script>
$(document).ready(function () {
$('#getLink').on("click", function () {
var url = $("#myUrl");
alert(url.val());
$.ajax({
type: "GET",
url: url.val(),
dataType: 'html',
success: function (data, textStatus, xhr) {
$("#data").text(textStatus);
},
error: function (data,textStatus, errorThrown){
$("#data").text(errorThrown);
}
});
});
});
</script>
<input id="myUrl" name="myURL" type="text" value="http://localhost:35460/Home/TestPage.cshtml" />
<input type="button" value="getLink" id="getLink">
<span id="data"></span>

jQuery AJAX not receiving JSON from http request

I ame using html with some jQuery to try out some JSON requests. I did a bit of research and tried making something small just to test it out. but when i run the script in my browser(Google Chrome) i dont get anything besides my html/css stuff. here is the code:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
console.log("Test");
console.log($.get("https://prod.api.pvp.net/api/lol/euw/v1.1/summoner/by-name/kerrie?api_key=[key]"));
</script>
*[key] is my key from the api owners(not to be shared on the internet).
when i check the network tab it says "304, not modified" i dont if this has anything to do wit it.
I'm just starting with websites and JavaScript/jQuery any help would be helpfull.
For better understanding you can call ajax method as below
$.ajax({
url: 'https://prod.api.pvp.net/api/lol/euw/v1.1/summoner/by-name/kerrie?api_key=[key]',
type: 'GET',
dataType: 'JSON',
async: false,
error: function(){},
success: function(resp){
// resp variable will be your JSON responce
}
}
});

AJAX failing silently

I am having a problem with one of my AJAX requests. All of this was working earlier, then I moved some files around in folders, and it must have broken something, but I have been unable to figure out what may be wrong with my pathing.
What is happening
AJAX request seems to be being ignored, nothing goes to console or the network tab, responseText is "", and I put a break point in the first line of the PHP which is never hit. It goes straight from $.ajax to the first line in my error callback. Additionally, when this PHP code is called by going straight to the page, or cron, it works fine.
I have a very similar call later to another file in the same folder that is working just fine.
What I have tried
As mentioned before, I have looked at responseText, console output, network tab, and breakpoints in my PHP (which are never reached), and I have looked around Google and stackoverflow. If I intentionally change the name to a non-existing file, I promptly get an error in the console.
Code below
$.ajax({
type: "POST",
url: "PHP/myFile.php",
data: '',
success: function () {
//Success code
},
error: function (xhr) {
var response = xhr.responseText; // response is ""
//Error code
}
});
Any ideas?
Here is the code, later in the file, which (successfully) grabs code from a file in the same folder.
$.ajax({
type: "GET",
cache: false,
url: "PHP/myOtherFile.php",
dataType: 'json',
success: function (data) {
// Success code
},
error: function () {
// Error Code
}
});
Pretty basic AJAX calls, not sure what is going on :/
Error callback is called on http errors, but also if JSON parsing on
the response fails
From: Jquery ajax error callback
From your working file, the code dataType: 'json' im blindly assuming your doing some kind of json parsing.
Try just making the "myFile.php" a simple echo 'x'; script, and check the response text. If your get something, then its because your script contains JSON parsing code, but your not passing any data to the script anyways.
Hence, the error callback would be called because the script is trying to JSON parse nothing (i.e. data: '')
I cant suggest anything else, from reading your questions description, hopefully someone else figures it out. goodluck

How to stop timed out ajax call using JQuery

I have an ajax application, which has code something like this:
$.ajax({
type: "GET",
url: "data.txt",
beforeSend:function(){
},
success:function(response){
just update responsed data;
}
});
this call is made every second and it just updates the latest data from 'data.txt' which is being updated on server using cron job. Now since it's only function is to update latest data each second so I'll be more interested in the latest ajax call ; so how can I terminate old ajax call that has crossed 4 seconds but yet not completed so that I can reduce the server traffic. And any suggestion if using 'data.html' or 'data.php' instead of 'data.txt' would increase the application performance ? And which web server can perform better than Apache for ajax driven application ? I need these help urgently ; Please do help.
You could keep track of when your last successful update time was.
function NowMS() {return parseInt(new Date().getTime())}
dataLastUpdateT = -Infinity;
$.ajax({
type: "GET",
url: "data.txt",
success: function(response){
if (NowMS() > dataLastUpdateT) {
use(response);
dataLastUpdateT = NowMS();
}
}
}
I don't know how you have it setup at the moment but perhaps it would be better to run your next AJAX call after the latest one completed (or returned an error). So it would be something like:
(function updateData() {
$.ajax({
type: 'GET',
url: 'data.txt',
beforeSend: function() {
// Do stuff
},
success: function(response) {
// Handle response
updateData();
}
});
})();
I don't know if there is any performance changes in changing the file type.
Edit: If you do need to just kill the request, you can do so using the technique explained here.
You could try this:
function getData(){
$.ajax({
type: "GET",
url: "data.txt",
timeout:4000,
beforeSend:function(){
},
success:function(response){
},
error:function(){
},
complete:function() {
setTimeout(function(){getData();},1000);
}
});
}
getData();
this way the ajax request timeouts after 4 seconds and retries each second (regardless of success or timeout)
Also have a look at nginx for example, it is fast and uses less memory than apache to handle client connections

Categories

Resources