最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - success ajax: if is 200 status code run function else another function - Stack Overflow

programmeradmin5浏览0评论

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
Add a ment  | 

3 Answers 3

Reset to default 4

Try 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);

发布评论

评论列表(0)

  1. 暂无评论