【发布时间】:2014-05-30 12:44:05
【问题描述】:
我已经在 Google 上搜索了我能找到的所有此错误实例,但没有一个解决方案适合我。本质上,我正在做其他人建议的事情。尽管如此,我收到一个错误,即我的回调函数没有被调用。我正在使用 Ajax/Jquery/JASONP 使用 GET 进行跨域表单提交。这是我在表单方面的代码:
$(document).ready(function() {
$("#ajaxform1").submit(function(e){
e.preventDefault();
var surl = "http://blahblah/test_create_user.php?callback=?";
$.ajax({
type: 'GET',
url: surl,
crossDomain: true,
data: $('#ajaxform1').serialize(),
dataType: "jsonp",
success: function(msg) {
$.each(msg, function (index, value) {
if(value==1)
{
alert("New User Added");
} else {
alert("User Already Exists")
} }
});
},
error: function(xhr, status, error) {
var myerror = xhr+" "+status+" "+error;
alert("Failure Connecting to Kayako. Please Try Again("+myerror+")"); }
});
这是我的 PHP 代码的适用 sn-p:
if($USER_CHK->first()->id){
$data = array('msg' => '0'); // user exists
else {
$data = array('msg' => '1'); // User added
}
//echo customerAdded(FALSE);
header('Access-Control-Allow-Origin: '.$_SERVER['HTTP_ORIGIN']);
header('Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS');
header('Access-Control-Max-Age: 1000');
header('Access-Control-Allow-Headers: Content-Type, Authorization,X- Requested- With');
header("Content-type: application/json");
$data = array('msg' => '0');
print $_REQUEST['callback']. '('.json_encode($data).')';
exit;
});
});
我的调试显示所有表单变量都已发布。 PHP 代码返回:jQuery11020900643879813015_1397599599587({"msg":"1"}) 然而,表明回调未被调用的错误被抛出。
【问题讨论】:
-
我不确定,但回复
content-type应该是application/x-javascript -
谢谢Shakib。我改了,还是没有奖品。