I don't know why doesn't work... i use $.ajax
for run file.php and pass it (POST
) a value of input
This file.php works but my function ajax doesn't work:
$.ajax({
type: 'POST',
url: 'file.php',
data: { email: $('#andressemail').val() },
success: function(data,status){
if(status === '200'){
newmessage();
}
else{
erroremessage();
}
}
});
funtion newmessage(){
alert('ok');
}
funtion erroremessage(){
alert('no');
}
The file.php works fine (it adds an user at my newsletter), but for $.ajax
doesn't work and status code is not 200
Why ?
I don't know why doesn't work... i use $.ajax
for run file.php and pass it (POST
) a value of input
This file.php works but my function ajax doesn't work:
$.ajax({
type: 'POST',
url: 'file.php',
data: { email: $('#andressemail').val() },
success: function(data,status){
if(status === '200'){
newmessage();
}
else{
erroremessage();
}
}
});
funtion newmessage(){
alert('ok');
}
funtion erroremessage(){
alert('no');
}
The file.php works fine (it adds an user at my newsletter), but for $.ajax
doesn't work and status code is not 200
Why ?
Share Improve this question asked Apr 29, 2016 at 12:29 BorjaBorja 3,5797 gold badges38 silver badges74 bronze badges 1- try this: status == 'success' – Mehdi Dehghani Commented Apr 29, 2016 at 12:31
3 Answers
Reset to default 4Try following to get status code, use xhr.status for statuscode:
$.ajax({
type: 'POST',
url: 'file.php',
data: { email: $('#andressemail').val() },
success: function(xml, textStatus, xhr) {
alert(xhr.status);
if(xhr.status === '200'){
newmessage();
}
else{
erroremessage();
}
}
});
funtion newmessage(){
alert('ok');
}
funtion erroremessage(){
alert('no');
}
The success function only runs when the HTTP response is already 200
. You need to use error function as well which fires when the HTTP response is not finished correctly. Change your code to look like:
function newmessage(data, textStatus, jqXHR){
alert('ok');
}
function erroremessage(data, textStatus, jqXHR){
alert('no');
}
$.ajax({
type: 'POST',
url: 'file.php',
data: { email: $('#andressemail').val() },
success: newmessage,
error: erroremessage
});
Best way to deal with $.ajax
is this:
var _ajax = function(url, data, type){
return $.ajax({
type: type,
url: url,
data: data,
});
}
var data = { email: $('#andressemail').val() };
_ajax('file.php', data, 'POST')
.success(function(res, statusTitle, xhr){
// res: response from server
// statusTitle = 'success', ...
// xhr: XHR object, for your case, xhr.status will be 200
});
you can use .error(function(){...})
too, (also .done
);
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744180404a4561954.html
评论列表(0条)