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

I don't know why doesn't work... i use $.ajax for run file.php and pass it (POST) a value of

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744180404a4561954.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信