前端:
<script type="text/javascript">
jQuery("#FormButtonRun").click(function(event){
submitEmailForm();
});
var submitEmailForm = function () {
var submitdata = {'Email':jQuery('#FormEmail').val(), 'fName':jQuery('#FormName1').val(), 'lName':jQuery('#FormName2').val(), 'listnews':"Yes"};
jQuery.ajax({
type:'POST',
url:'https://dev.formserver.com/ajax.php',
data:submitdata,
success: submitwrkd,
error: submitfaild,
dataType:'json'
});
}
var submitwrkd = function(response, more, xhr){
alert("It worked!);
alert(response.msg);
}
var submitfaild = function(response, more, xhr){
alert("allegedly that failed");
}
后端:ajax.php:
<?php
header('Content-type:application/json');
echo json_encode(array('success'=>true, 'msg'=>'It was a success'));
?>
Content-Control-Allow-Origin在内容服务器和发布到服务器上都设置为*
完整标题:
POST /ajax.php HTTP/1.1
Host: dev.dataserver.org
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:51.0) Gecko/20100101 Firefox/51.0
Accept: application/json, text/javascript, */*; q=0.01
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate, br
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Referer: https://dev.dataserver.org/popuptest
Content-Length: 127
Origin: https://dev.contentserver.org
Connection: keep-alive
Email={removed post data}
响应:
HTTP/1.1 200 OK
Server: nginx/1.8.1
Date: Tue, 16 May 2017 00:44:49 GMT
Content-Type: application/json
Transfer-Encoding: chunked
Connection: keep-alive
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: POST
数据:{"success":true,"msg":"It was a success"}
"POST /ajax.php HTTP/1.1" 200 67 "-" "Mozilla/5.0 ...
success: true, msg: 'It was a success'
javascript警告宣布:allegedly that failed
我已经检查了内容控件头Access-Control-Allow-Origin: *; Access-Control-Allow-Methods: POST;
,显然它经历了,数据类型,json错误,javascript错误(没有抛出),不同的浏览器......
如何在成功时运行定义的success
函数,并在出现错误时运行错误函数?
我可以回到.done(),但我们强烈希望知道它有效。
答案 0 :(得分:0)
Access-Control-Allow-Origin: '*
)