简体   繁体   English

即使成功执行后,Ajax jQuery也不会调用成功

[英]Ajax jquery not calling success even after successfully executing

I need to pass json variable using AJAX. 我需要使用AJAX传递json变量。 Here is my ajax function: 这是我的ajax函数:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>

function emailchkforfrntend()
{
var key='7bc443a2a363b050bc94963baff4b2ac';
var email_id=$("#email_id").val();// email_id->input field id

$.ajax({

    url: "targetURL/example.php",
    type : "post",
    dataType: "jsonp",
    data : { "email_id": email_id, "key": key},
    success: function(result){
        alert('success');
    },
    error: function(XMLHttpRequest, textStatus, errorThrown){
        alert('error '+errorThrown);
        alert('status '+textStatus);
        alert('response text '+XMLHttpRequest.responseText);
    },
    complete : function(){
        alert('in complete');
    }

})
.done(function(msg) {
    alert('Inside done....'+msg);
if(msg!=0)
{
var obj = jQuery.parseJSON(msg);

//var p_fname=obj.p_fname; 

$("#Result_Div").html("<b style='color:blue;'>already existing Member <i class='fa fa-thumbs-o-up'></i></b>");

}
else
{

    $("#Result_Div").html("<b style='color:green;'>New Member <i class='fa fa-thumbs-o-up'></i></b>");

}
});
}

HTML code: HTML代码:

<input type="text" id='email_id' onkeyup="emailchkforfrntend();">

After calling to this function, everytime it is giving call to error function and the control is not calling .done(function(msg){}) . 调用此函数后,每次都调用错误函数,而控件未调用.done(function(msg){}) Also I am getting Jquery1112016992787341587245_1466413029814 was not called in first alert of error function. 我也得到Jquery1112016992787341587245_1466413029814 was not called在错误功能的第一警报中Jquery1112016992787341587245_1466413029814 was not called I passed one dummy PHP script to URL, which inserts record into database, to check whether is it calling the URL or not, the call to that PHP is working but still it is calling error function instead of calling success function. 我向URL传递了一个虚拟的PHP脚本,该脚本将记录插入数据库中,以检查是否正在调用URL,对该PHP的调用是否正常,但仍在调用错误函数而不是调用成功函数。

Is there anything that I am missing? 有什么我想念的吗? what is supposed to be done in order to work this functionality? 为了使用此功能应该做什么?

Trying to help... You said you need send JSON variables to PHP using ajax . 尝试提供帮助...您说过需要使用ajaxJSON变量发送到PHP I noticed that you are using JSONP . 我注意到您正在使用JSONP Is JSONP really necessary? JSONP真的有必要吗?

I wrote a HTML/JS code which is working. 我写了一个可以正常工作的HTML/JS代码。 The code just send data using AJAX and execute the callbacks. 该代码仅使用AJAX发送数据并执行回调。 I'm wondering if in your case the JSONP is not affecting the complete callback. 我想知道在您的情况下JSONP是否不会影响完整的回调。

Result: 结果:

Tue Jun 21 2016 15:39:54 GMT-0300 (BRT) Req sent {"email_id":"1","key":"abc"}
Tue Jun 21 2016 15:39:54 GMT-0300 (BRT) Success
Tue Jun 21 2016 15:39:54 GMT-0300 (BRT) Complete {"readyState":4,"responseText":"\n\t\n\t\t\n\t\t\n\t\n\t\n\t\tResult:\n\t\t
\n\t\n","status":200,"statusText":"OK"}

Code: 码:

<html>
    <head>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
        <script>
            function log(msg) {
                var dateMsg = (new Date()) + ' ' + msg;
                var $res = $('#result');
                var $o = $('<li>').html(dateMsg);
                $res.append($o);
            }

            function checkEmailFrontEnd(email_id, key) {
                var userdata = {
                    email_id: email_id,
                    key: key
                };
                var opts = {
                    url: 'html.html',
                    type: 'POST',
                    data: userdata,
                    success: function(data, textStatus, jqXHR){
                        log('Success');
                    },
                    error: function(jqXHR, textStatus, error){
                        log('Error ' + error);
                    },
                    complete: function(jqXHR, textStatus){
                        log('Complete ' + JSON.stringify(jqXHR));
                    }
                };
                $.ajax(opts);   
                log('Req sent ' + JSON.stringify(userdata));
            }
            $(function(){
                checkEmailFrontEnd('1', 'abc');
            });
        </script>
    </head>
    <body>
        Result:
        <ul id='result'></ul>
    </body>
</html>

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

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