Javascript ajax是否需要contentType发送到php?如果是,为什么会出错?
我一直在试图找出这个问题,但我似乎没有找到错误,而是在我的Javascript ajax是否需要contentType发送到php?如果是,为什么会出错?,javascript,php,jquery,sql,ajax,Javascript,Php,Jquery,Sql,Ajax,我一直在试图找出这个问题,但我似乎没有找到错误,而是在我的脚本中找到的 $('#Bshift').click(function(){ var isValid=false; isValid = validateForm(); if(isValid) { var ArrId= <?php echo json_encode($arrId ); ?>; var ArrQty= <?php echo json_encode($arrQty ); ?>; var
脚本中找到的
$('#Bshift').click(function(){
var isValid=false;
isValid = validateForm();
if(isValid)
{
var ArrId= <?php echo json_encode($arrId ); ?>;
var ArrQty= <?php echo json_encode($arrQty ); ?>;
var counter= <?php echo json_encode($i ); ?>;
var productId;
var productQty;
for (i = 0; i < counter; i++) {
productQty = ArrQty[i];
productId= ArrId[i];
var pLocal= document.getElementById(productId).value;
var prodData = 'pLocal=' + pLocal+ '&proId='+productId;
$.ajax ({
url: 'shiftSave.php',
type: 'POST',
data: prodData,
dataType: 'json',
contentType: "application/json; charset=utf-8", // this is where have the error
});
}
var pettyCash= document.getElementById("pettyCash").value;
var comment= document.getElementById("comment").value;
var prodData1 = 'pettyCash=' + pettyCash+ '&comment='+comment;
$.ajax ({
url: 'shiftSave.php',
type: 'POST',
data: prodData1,
dataType: 'json',
contentType: "application/json; charset=utf-8 ", // Error here too
}).done(function(data){
alert("Data Saved. Shift Started.");
document.getElementById("register").reset();
document.getElementById("Bshift").disabled = true;
document.getElementById("StartingB").disabled = true;
}).fail(function(error){
alert("Data error");
});
}
});
我的脚本
$('#Bshift').click(function(){
var isValid=false;
isValid = validateForm();
if(isValid)
{
var ArrId= <?php echo json_encode($arrId ); ?>;
var ArrQty= <?php echo json_encode($arrQty ); ?>;
var counter= <?php echo json_encode($i ); ?>;
var productId;
var productQty;
for (i = 0; i < counter; i++) {
productQty = ArrQty[i];
productId= ArrId[i];
var pLocal= document.getElementById(productId).value;
var prodData = 'pLocal=' + pLocal+ '&proId='+productId;
$.ajax ({
url: 'shiftSave.php',
type: 'POST',
data: prodData,
dataType: 'json',
contentType: "application/json; charset=utf-8", // this is where have the error
});
}
var pettyCash= document.getElementById("pettyCash").value;
var comment= document.getElementById("comment").value;
var prodData1 = 'pettyCash=' + pettyCash+ '&comment='+comment;
$.ajax ({
url: 'shiftSave.php',
type: 'POST',
data: prodData1,
dataType: 'json',
contentType: "application/json; charset=utf-8 ", // Error here too
}).done(function(data){
alert("Data Saved. Shift Started.");
document.getElementById("register").reset();
document.getElementById("Bshift").disabled = true;
document.getElementById("StartingB").disabled = true;
}).fail(function(error){
alert("Data error");
});
}
});
当我在没有contentType的情况下执行时,它会变为true,但会失败并给我数据错误(我在函数fail上使用的警报),但如果我使用contentType,它会转到函数。完成,然后通过,但查询不会执行。您还需要将发送的数据字符串化
data: JSON.stringify(prodData1),
您可以创建一个helper函数,您可以在任何地方使用它来发布JSON文章
function jsonPost(url, data, success, fail) {
return $.ajax({
url: url,
type: "POST",
dataType: "json",
contentType: "application/json; charset=utf-8",
data: JSON.stringify(data),
success: success,
error: fail
});
}
ajax是否需要contentType发送到php
您有一个contentType:“application/json;charset=utf-8”,
在请求有效负载中发送数据
,但是如果您从ajax中省略/删除它,那么默认情况下是application/x-www-form-urlencoded;字符集=UTF-8
让我们在这里看到它们:
内容类型为application/json的请求可能如下所示:
POST /some-path HTTP/1.1
Content-Type: application/json
{ "foo" : "bar", "name" : "John" }
如果您提交的HTML表单包含method=“POST”
和内容类型:application/x-www-Form-urlencoded
(jquery ajax的默认设置)或内容类型:多部分/表单数据
,则您的请求可能如下所示:
POST /some-path HTTP/1.1
Content-Type: application/x-www-form-urlencoded
foo=bar&name=John
因此,如果您在请求有效负载中发送数据,该数据可以通过内容类型:application/json
发送,那么您就不能使用php SuperGlobals(如$\u POST
)获取这些发布的值
您需要使用file\u get\u contents('php://input)
首先删除内容类型选项。让jQuery使用默认值。
第二,
更改:
var pLocal= document.getElementById(productId).value;
var prodData = 'pLocal=' + pLocal+ '&proId='+productId;
致:
以及:
致:
ajax是否需要contentType发送到php?如果是,为什么会出错?
var pLocal= document.getElementById(productId).value;
var prodData = { pLocal: pLocal, proId: productId };
var pettyCash= document.getElementById("pettyCash").value;
var comment= document.getElementById("comment").value;
var prodData1 = 'pettyCash=' + pettyCash+ '&comment='+comment;
var pettyCash= document.getElementById("pettyCash").value;
var comment= document.getElementById("comment").value;
var prodData1 = { pettyCash: pettyCash, comment: comment };