前端:
<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'));
?>内容-控制-允许-原产地设置为*的内容-服务器和张贴-到服务器
完整标题:
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(),但是有一种强烈的偏好,那就是知道它有效。
发布于 2017-05-26 19:53:54
Access-Control-Allow-Origin: '*)https://stackoverflow.com/questions/43990715
复制相似问题