我需要使用AJAX传递json变量。这是我的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:
<input type="text" id='email_id' onkeyup="emailchkforfrntend();">
调用此函数后,每次调用error函数时控件都不会调用.done(function(msg){})
。此外,我在错误功能的第一个警报中得到Jquery1112016992787341587245_1466413029814 was not called
。我将一个虚拟的PHP脚本传递给URL,它将记录插入到数据库中,以检查是否调用了URL,对该PHP的调用是否正常,但仍调用错误函数而不是调用成功函数。
我有什么遗失的吗?为了实现这个功能应该做些什么?
答案 0 :(得分:0)
尝试帮助...您说您需要使用JSON
向PHP
发送ajax
个变量。我注意到您正在使用JSONP
。 JSONP
真的有必要吗?
我写了一个HTML/JS
代码正在运行。代码只使用AJAX
发送数据并执行回调。我想知道在你的情况下JSONP
是否影响完整的回调。
<强>结果:强>
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"}
<强>代码:强>
<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>