When i click run button show this error in chrome console and don’t alert anything
POST http://mysite/gd/add.php 503 (Service Unavailable)
index.php
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
</head>
<body >
<script>
function go(){
$.ajax({ url: 'add.php',
data: {'q': ''},
type: 'post',
success: function(output) {
alert(output);}
});
}
</script>
<button onclick="go();">Run</button>
</body>
</html>
add.php
<?php echo "Hello"; ?>
asked Nov 6, 2013 at 8:03
4
I have tried your code in local environment, it is working fine. The reason for 503 error because of the Web server (running the Web site) is currently unable to handle the HTTP request due to a temporary overloading or maintenance of the server. The implication is that this is a temporary condition which will be alleviated after some delay. Some servers in this state may also simply refuse the socket connection, in which case a different error may be generated because the socket creation timed out.
user2511140 : I add this code to show errors.my server is to busy and show error.i changed server and problem solved
$.ajax({ url: 'add.php',
type: 'post',
data: {'q': ''},
success: function(output) {
alert(output);},
error: function (request, status, error) {
alert(request.responseText);},
});
}
user2511140
1,6683 gold badges26 silver badges32 bronze badges
answered Nov 6, 2013 at 8:15
Krish RKrish R
22.6k7 gold badges50 silver badges59 bronze badges
6
so i know it is old, but i just solved this problem while working on a wordpress project. I got the same error message, because there was a difference between the Url of the admin-ajax — script, written in the header(www.url.de/admin-ajax.ph) — and the url to load the data from (www.urx.de/ajax.php)
answered Aug 25, 2016 at 10:25
Your current page is a.php
and you send ajax request to add.php
, so check whether they both are in same directory or not?
If so then try following or else try absolute HTTP path.
$.ajax({ url: './add.php',
data: {'q': ''},
type: 'POST',
success: function(output) {
alert(output);
}
});
answered Nov 6, 2013 at 8:09
ParixitParixit
3,8293 gold badges37 silver badges61 bronze badges
4
I think you should call with http://mysite/gd/add.php
or gd/add.php
URL
$.ajax({ url: 'http://mysite/gd/add.php',
data: {'q': ''},
type: 'post',
success: function(output) {
alert(output);}
});
If above code is not working. Please check your .htaccess
file.
answered Nov 6, 2013 at 8:08
KKKKKK
1,6527 gold badges29 silver badges49 bronze badges
2
При попытке добавить коммент через Ajax сайт мертво встает(зависает) логи показывают ошибку 503 при отправки /comment/add . раньше все работало проблема в сервере или же в коде? прошу помочь
$(this).ajaxSubmit({
url : baseUrl + 'comment/add',
type : 'POST',
dataType : 'json',
success : function(data) {
var json = data;
if (json.status == 0) {
show_comment_add_error(form,json.message);
} else {
div = $("<div style='display: none'></div>");
div.html(json.comment);
//commentList.append(div);
$(".comment-lists-" + form.data('type') + '-' + form.data('type-id')).each(function() {
$(this).append(json.comment);
//alert(".comment-lists-" + form.data('type') + '-' + form.data('type-id'))
});
$(".comment-count-"+form.data('type') + '-' + form.data('type-id')).each(function() {
$(this).html(json.count);
})
notifySuccess(json.message);
resent_comment_form(form);
reloadInits();
}
toogleCommentIndicator(form);
},
error : function() {
toogleCommentIndicator(form);
}
});
return false;
});
Error handling in AJAX
- Digital Engineering
Error handling in AJAX
Some times when we request to server we didn’t get proper response (HTTP Status 200) as per our expectation. For ex. we can get
1) Internal Server Error
2) Network Connection Error
3) Page Not Found Error,
4) Access Denied Error.
So if we are making call to server via http post request/simple http request directly then user will see these error in browser. But when we use AJAX service calls and if we don’t properly handle the inevitable errors (like mentioned above) then our code fails and it didn’t display anything and user get stucked. AJAX service calls are especially problematic. So to show proper error message to user we need to use proper error handling.
Even when a client-side error is thrown, most users won’t notice it and the ones who do notice won’t know what the error means or what to do next. In fact, many developers don’t notice client-side scripting errors that occur while they’re debugging their own applications!
For example : After logined in the system user open site link in new tab. Now user has opened two tab and if user logout from first tab and switch to second tab then in second tab whenever user will not refresh that page he will not be logged out. And without refresh the page if user fire an event that is AJAX based and if we didn’t used proper error handling user will get stucked because he will not get proper message, but we must show him a proper message that “Sorry!! Your session has expired. Please login again.” . Same may happen when session expired automatically after not using the system for few minutes.
So to come over this problem we will use AJAX error function.
).error(function (jqXHR, textStatus, errorThrown) {
alert(formatErrorMessage(jqXHR, errorThrown));
|
So when ever we didn’t get “HTTP Status 200 OK” from server and get any error this response will automatically will go to error function.
1) jqXHR : This variable will contain status code and we can access by “jqXHR.status”.
2) textStatus : Response in text like Success.
3) errorThrown : Some time error may be related to parse error, timeout error so these error message will come under this variable.
alert(formatErrorMessage(jqXHR, errorThrown));
|
1) alert : Simple. using to show error message
2) formatErrorMessage : We are using this function to parse this variable and get exact error and to show a proper message to user.
Its defined below.
function formatErrorMessage(jqXHR, exception)
if (jqXHR.status === 0) {
return (‘Not connected.\nPlease verify your network connection.’);
} else if (jqXHR.status == 404) {
return (‘The requested page not found.’);
} else if (jqXHR.status == 401) {
return (‘Sorry!! You session has expired. Please login to continue access.’);
} else if (jqXHR.status == 500) {
return (‘Internal Server Error.’);
} else if (exception === ‘parsererror’) {
return (‘Requested JSON parse failed.’);
} else if (exception === ‘timeout’) {
return (‘Time out error.’);
} else if (exception === ‘abort’) {
return (‘Ajax request aborted.’);
return (‘Unknown error occured. Please try again.’);
|
Now how to handle errors in jquery load function.
$(href).load(url , function(result , textStatus, jqXHR){
// Type for code to parse result
alert(formatErrorMessage(jqXHR, ‘nothing’));
|
Now in “HTTP Status 401 Not Authorized condition” if you want to redirect user to login page.
use
).error(function (jqXHR, textStatus, errorThrown)
bootbox.alert(formatErrorMessage(jqXHR, errorThrown), function() {
window.location.href = websiteUrl+‘/login’;
|
Note : bootbox.alert is a “bootboxjs” function to show alert message with proper bootstarp design popup.
http://bootboxjs.com/
You can also update error message in “formatErrorMessage” function as per your requirement and can add more exception and status code.
1) 400 : “Server understood the request, but request content was invalid.”
2) 403 : “Forbidden resource can’t be accessed.”
3) 503 : “Service unavailable.”
Also if you want to set your own “HTTP Status Code” according to your condition at server side you can use “header” function.
Ex : header(“HTTP/1.1 401 Unauthorized”);
Thanks for reading.
Page load link
Go to Top
My app works fine on my local machine. I’m able to perform all Ajax calls perfectly. Tested thoroughly on several web browsers and everything works seamlessly on localhost. Problems are now happening on live server. While some Ajax calls work, the most important one which is register.php does not. Upon contacting my host, they told me they can’t help me since it’s outside their scope which is fine I’m on my own. But is a 503 not something they should handle? They told me the php.ini is read-only(shared hosting)… So does it mean I’m doom or what am I doing wrong here? Php is activated on livesite and I also have a live database(mariadb). I’m using phpmailer in the register.php script but they told me I was allowed to use it. Any help?
This is the error I receive in the console on live site :
asked Aug 17, 2018 at 16:09
1
Since it is a 503 server-side error I would try re-uploading the problem files since the other files seem to be working. Maybe some files got corrupted during transfer. Let us know if you solve it and what the problem was.
answered Aug 17, 2018 at 16:23
June 26, 2012 by Norberto Burciaga
INTRODUCTION
Developing a web page with php code, javascript and jquery, the page has the following code:
$x.post(“<?php echo Mage::getBaseUrl()?>keepsakes/select/saveProject”, {projectid: projectId, thumburl: thumbUrl, media: projectMedia, savedprojectid: ‘<?php if (isset($savedProjectId)) echo $savedProjectId;?>’});
Basically it is an AJAX call to an action, and it is passing parameters with POST method. projectId, thumbUrl and projectMedia are variables that are obtained from a javascript callback function, but savedProjectId comes with the page when the php code is generated from the server.
PROBLEM
The x.post AJAX call is getting “503 Service Unavailable” error in specific cases: using FireFox and only in some of the environments and only in one situation.
DESCRIPTION
This is the kind of error that are very difficult to track, because it fails only in very specific conditions. This code is being called two times in the code, in one situation it is called alone, and in the other situation is called and then a redirection to another page is made.
Only in the situation where this code is called in conjunction with the redirection in firefox and in some specific environments fails.
Local Environment | Development Environment | Quality Assurance Environment | |
Firefox | passed | passed | failed |
Internet Explorer | passed | passed | passed |
Chrome | passed | passed | passed |
SOLUTION
Mixing different languages like php and Javascript can make that you think that something is correct but it is not for the specific language, In this case the problem is the single quotes surrounding the php code for the parameter ‘<?php if (isset($savedProjectId)) echo $savedProjectId;?>‘ those single quotes are not php anymore, they are javascript, and the javascript is passing the other parameters with double quotes and this parameter with single quotes, so the solution was to change these single quotes with double quotes, and now getting the 200 status from the server meaning that the call is correct.
CONCLUSION
To solve a 503 error is very complicate because there are many causes that generate this error.It is needed to be very aware when using multiple languages in a single page because it can looks correct at first sight, but really incorrect.
REFERENCES
Posted in Web Development | Tagged 503, ajax, firefox, javascript, jquery, page, php, post, web | Leave a Comment