我在AJAX jQuery中使用beforeSend来传递REST服务中的头值。
标头值在Internet Explorer 8中很好地传递。但是在Firefox中不传递标头值,并且不调用服务。
这是我的代码:
var postCall = function () {
$.support.cors = true;
var HFAssociateRefId = document.getElementById('MainContent_HFAssociateRefId').value;
var Input = {
AssociateRefId: HFAssociateRefId
};
alert(JSON.stringify(Input));
var url = document.URL;
var currentdate = new Date();
var datetime = (currentdate.getMonth() + 1) + "/"
+ currentdate.getDate() + "/"
+ currentdate.getFullYear() + " "
+ currentdate.getHours() + ":"
+ currentdate.getMinutes() + ":"
+ currentdate.getSeconds();
$.ajax({
type: "POST",
headers: { Accept: "text/plain; charset=utf-8", "Content-Type": "text/plain; charset=utf-8"
},
beforeSend: function (xhr, settings) {
xhr.setRequestHeader("Date", datetime);
xhr.setRequestHeader("URL", url);
},
url: "http://localhost:40680/LinkService.svc/TokenInsertion",
data: JSON.stringify(Input),
contentType: "application/json",
dataType: "json",
success: function (response) {
alert(response);
},
error: function (xhr, status, error) {
alert(status);
},
});
我还尝试通过调用xhr作为此link中指定的新XMLHttpRequest。
但它在Firefox中不起作用。 ??
提前谢谢。
答案 0 :(得分:1)
看起来Firefox不尊重标题Date
。它正在发送标头URL
。我无法找到任何资源来解释这种行为。
作为解决方案,您可以将标题Date
重命名为其他内容。
Chrome也会显示相同的行为。
在进一步调查中,它看起来像standard的标准行为。请查看标题为Terminate these steps if header is a case-insensitive match for one of the following headers:
同样在this question中提出。
答案 1 :(得分:0)
type: "post",
data: JSON.stringify(Input), <=== this is fail
correct is: data:{data:JSON.stringify(Input)},
recuest to $_POST['data']; is better if using arrays...
我建议您尝试使用jquery ...这个例子非常简单。
基本样本!
第1页到Html或PHP不是很重要这个名字......
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Prueba Array 1</title>
**<!-- important download JQUERY 1.3 or late... -->**
<script type="text/javascript" src="js/jquery-1.3.min.js"></script>
<script>
$(document).ready(function(){
function toSend(d) {
$.ajax({
type: "get", **// this is posible chain by "post"**
url: 'test.php',
data: {datos:JSON.stringify(d)},
dataType: "json",
success: function(test) {
//console.log("visualizacion del registro Name[1]: "+ test.names[1])
console.info("Array Send");
$.each( test, function( key, value ) {
console.log( key + ": " + value );
});
}
});
}
// send Submit
$(":send").live('click',function(){
console.log("preparing to send! ");
// lista de entradas en un ID
var data = $('#form_1 input').serializeArray();
// Serializacion de las entradas input
// // Acciones
toSend(data);
});
});
</script>
</head>
<body>
<div id="ie">
<form id="form_1" action="#" >
<input name="Nombre" type="text" value="Petronila">
<input name="Apellido" type="text" value="Sinforosa">
<input name="Telefono" type="text" value="phone">
<input name="Ciudad" type="text" value="Living in Caracas">
<input type="submit" value="Send">
</form>
</div>
</body>
</html>
到下一步,复制此代码,然后将其保存为test.php!并运行
<?php
if(isset($_GET['datos'])){
$names = array('john doe', 'JANE doe');
$ids = array('123', $datos);
$data['names'] = $names;
$data['ids'] = $ids;
echo json_encode($data);
}else{
$error = array('error','No se recibieron metodos');
$error = array('messeng','Method need chain test to Get');
echo json_encode($error);
}
?>
确定非常非常重要的是在浏览器控制台F12中检查结果以检查其中任何一个,你需要激活控制台firefox有时候 我测试,没关系!
答案 2 :(得分:0)
参考link。 Firefox和Chrome从不接受要添加的新自定义标头,因为它违反了浏览器安全规则。