0

I need to pass json variable using AJAX. Here is my ajax function:

<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:

<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){}). Also I am getting Jquery1112016992787341587245_1466413029814 was not called in first alert of error function. 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.

Is there anything that I am missing? what is supposed to be done in order to work this functionality?

1 Answers1

0

Trying to help... You said you need send JSON variables to PHP using ajax. I noticed that you are using JSONP. Is JSONP really necessary?

I wrote a HTML/JS code which is working. The code just send data using AJAX and execute the callbacks. I'm wondering if in your case the JSONP is not affecting the complete callback.

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>
Valdek Santana
  • 337
  • 1
  • 8