简体   繁体   English

检查ajax响应javascript

[英]Check ajax response javascript

I have an ajax form on a page that receives a true or false response. 我在页面上有一个ajax表单,它接收真或假的响应。 What I am trying to do is check if the response exists or not and run javascript based on that, but I am a little stuck. 我要做的是检查响应是否存在并根据它运行javascript,但我有点卡住了。

$( "#button" ).click(function() {

// if previous ajax response = true
// do nothing
// if previous ajax response is not = true or does not exist
   dosomething();

});

ajax: AJAX:

                success:function(response){
                //log on success
                console.log(response);
                if(response == "true"){dosometing();}
                    if(response == "false"){alert("some alert");}
            },
            error:function(jqXHR,error){
                //write the error log
                console.log(jqXHR.status + " " + error);
            }

Maybe I'm not asking the right question. 也许我不是在问正确的问题。

EDIT 2: the ajax form is actually submitted elsewhere on the page. 编辑2:ajax表单实际上是在页面的其他地方提交的。 I'm just trying to see if it was called or not at some other point on the page. 我只是想看看它是否在页面的其他位置被调用。

Try this out to check response has set. 试试这个来检查响应是否已设定。

if($.plainObject(response)){
    doSomething();
}

if your web method return a value (exp: bool) you will find it in response.d 如果你的web方法返回一个值(exp:bool),你会在response.d中找到它

success:function(response){
                    //log on success
                    console.log(response.d);
                    if(response.d == "true"){dosometing();}
                        if(response.d == "false"){alert("some alert");}
                },
                error:function(jqXHR,error){
                    //write the error log
                    console.log(jqXHR.status + " " + error);
                }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM