s***@hotmail.com
s***@hotmail.com
  • 发布:2016-05-26 10:56
  • 更新:2017-08-17 16:20
  • 阅读:1394

【报Bug】ajax用例程可以返回结果,用本机写的php就报abort

分类:MUI

详细问题描述
[内容] 用MUI官方例子直接改的,浏览器里Chrome会提交两次,查了资料说跨域有次握手,导致post不成功,总是返回未传递参数,用手机测试就是返回abort,status=0

重现步骤
[步骤]
[结果]
[期望]

运行环境
[系统版本]
WIN10
[浏览器版本]

[IDE版本]
[mui版本]

附件
[代码片段]
脚本:
<script>
(function($) {
var network = true;
if (mui.os.plus) {
mui.plusReady(function() {
if (plus.networkinfo.getCurrentType() == plus.networkinfo.CONNECTION_NONE) {
network = false;
}
});
}
var custnm = document.getElementById("custnm");
var telno = document.getElementById("telno");
var descr = document.getElementById("descr");
//成功响应的回调函数
var success = function(response) {
alert(response.result);
var dataType = "json";
if (dataType === 'json') {
response = JSON.stringify(response);
} else if (dataType === 'xml') {
response = new XMLSerializer().serializeToString(response).replace(/</g, "&lt;").replace(/>/g, "&gt;");
}
};
//设置全局beforeSend
$.ajaxSettings.beforeSend = function(xhr, setting) {
//beforeSend演示,也可在$.ajax({beforeSend:function(){}})中设置单个Ajax的beforeSend
console.log('beforeSend:::' + JSON.stringify(setting));
};
//设置全局complete
$.ajaxSettings.complete = function(xhr, status) {
console.log('complete:::' + status);
console.log(xhr.status);
console.log(xhr.readyState);
}
var ajax = function() {
//利用RunJS的Echo Ajax功能测试
var url = 'http://localhost/sfy/data.php';
//请求方式,默认为Get;
var type = "post";
//预期服务器范围的数据类型
var dataType = "json";
//允许跨域
var crossDomain = true;
var timeout = 10000;
//发送数据
var data = {
custnm: custnm.value,
telno: telno.value,
descr: descr.value
};
if (type === 'get') {
if (dataType === 'json') {
$.getJSON(url, data, success);
} else {
$.get(url, data, success, dataType);
}
} else if (type === 'post') {
$.post(url, data, success, dataType);
}
};
//发送请求按钮的点击事件
document.getElementById("confirm").addEventListener('tap', function() {
if (network) {
ajax();
} else {
mui.toast("当前网络不给力,请稍后再试");
}
});
})(mui);
</script>

本机php内容:
<?php
header('Content-type: text/json');
header('Content-type: text/html; charset=utf8');
header('Access-Control-Allow-Origin:*');
header('Access-Control-Allow-Methods: POST, GET');
header('Access-Control-Allow-Headers:X-Requested-With');
header('Cache-Control: no-cache');

$arr = array ('result'=>'未传递参数');

@$custnm = $_GET['custnm'];
@$telno = $_GET['telno'];
@$descr = $_GET['descr'];

if ($custnm == "" && $telno == "") {
echo json_encode($arr);
}else
if ($custnm != "" && $telno != "") {
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "sdb";

// 创建连接  
$conn = new mysqli($servername, $username, $password, $dbname);  

// 检测连接  
if ($conn -> connect_error) {  
    die("Connection failed: " . $conn -> connect_error);  
}  
// 设置编码  
$stmt = $conn -> prepare("set names 'utf8'");  
$stmt -> execute();  

// prepare and bind  
$stmt = $conn -> prepare("INSERT INTO disassemble (custnm, telno, descr) VALUES(?, ?, ?)");  
$stmt -> bind_param("sss", $custnm, $telno, $descr);  

$stmt -> execute();  

$stmt -> close();  
$conn -> close();  

$arr['result'] = "SUCCESS";     
echo json_encode($arr);  

} else {
$arr['result'] = "请填写参数";
echo json_encode($arr);
}
?>
[安装包]

联系方式
[QQ]
[电话]

2016-05-26 10:56 负责人:无 分享
已邀请:
BoredApe

BoredApe - 有问题就会有答案。

mui.ajax中添加这个参数试试
crossDomain:true//强制使用5+xhr跨域

  • s***@hotmail.com (作者)

    //允许跨域

    var crossDomain = true;

    写法不太一样,我这样加了的

    2016-05-26 14:59

  • 回梦無痕

    var crossDomain = true;之后呢?只是定义一个变量,并没有应用

    2017-08-17 16:49

1***@qq.com

1***@qq.com

解决了吗 我的就是数据库都改了 他还报这个错 加上 这个crossDomain:true 是没有报错了

可是提示信息没返回过来啊

该问题目前已经被锁定, 无法添加新回复