Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 将$.ajax()函数返回的值与字符串进行比较_Jquery_Ajax - Fatal编程技术网

Jquery 将$.ajax()函数返回的值与字符串进行比较

Jquery 将$.ajax()函数返回的值与字符串进行比较,jquery,ajax,Jquery,Ajax,如果零件不工作, var name = $("#name"); $.ajax({ type: "get", url: "test.jsp", data: "name="+name, success: function(msg) { if( msg == "available" ) { // you conditional code here

如果零件不工作,

var name = $("#name");

$.ajax({
    type:       "get",
    url:        "test.jsp",
    data:           "name="+name,
    success:    function(msg) {

        if( msg == "available" )
        {
            // you conditional code here
            // i want to display a image here with id nameInfo
            $('#result').hide();

            $("#result").html(msg).fadeIn("slow");
        }
    }
});

修剪您的响应代码并检查条件

 success: function(msg) {
                        if($.trim(msg) == '1') {
                            console.log("done");
                        } else {
                            console.log("fail");
                        }   

                }

如果(msg=='available'){}
?哎哟,一个卷发括号跑了,对不起。修正。什么是擅离职守。我无法理解这是一个来自军队的英语表达,代表“未经许可缺席”,基本上意思是“失踪”。在我的回答中,我漏掉了一个大括号,这打破了密码。替换了大括号后,它现在应该可以工作了。在ajax查询中,它应该是name.val()
$.ajax({
    type:       "get",
    url:        "test.jsp",
    data:           "name="+name,
    success:    function(msg) {

        if( msg == 'available' )
        {
            // you conditional code here
        }

        $('#result').hide();

        $("#result").html(msg)
        .fadeIn("slow");
    }
});
$.ajax({
    type:       "get",
    url:        "test.jsp",
    data:           "name="+name,
    success:    function(msg) {

        // msg is returned by the ajax function
        // this holds the returned data

        alert(msg);

        // Compare the msg variable to see if it returned condition
        // this compares the ENTIRE text of return, if it has any more
        // characters in it, this condition will never be met!
        if(msg == "available"){
            alert("Yes!");
        }else{
            alert("No :-(  ");   // Sad face :(  The return was something different
        }

    }
});
 success: function(msg) {
                        if($.trim(msg) == '1') {
                            console.log("done");
                        } else {
                            console.log("fail");
                        }   

                }
 $.ajax(
         {
          type: "POST",
          url: "test.php",
          data: $('#frmlogin').serialize()
        }
         ).done(
            function( resp ) {
                //alert(resp.indexOf("Email"));
                if(resp.indexOf("Email") > -1){
                    alert("Email text available on response");

                } else {
                    alert("Email text not available on response");   
                }
            }
        );