详细问题描述
[内容] 用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, "<").replace(/>/g, ">");
}
};
//设置全局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]
[电话]
s***@hotmail.com (作者)
//允许跨域
var crossDomain = true;
写法不太一样,我这样加了的
2016-05-26 14:59
回梦無痕
var crossDomain = true;之后呢?只是定义一个变量,并没有应用
2017-08-17 16:49