我一直在试图找出这一个,但我似乎没有找到错误,但在我的script
My script
$('#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
脚本去完成,但如果我把它拿走,然后我的sql在我的php执行并给我一个响应
Php code shiftSave.php
<?php
include "connection.php";
session_start();
$data=array();
$location="";
if (isset($_SESSION['location'])) {
$location=$_SESSION['location'];
}
if (isset($_SESSION['eId'])) {
$empId=$_SESSION['eId'];
}
if(@$_POST['pLocal']) {
$proQty = $_POST['pLocal'];
$proid = $_POST['proId'];
try {
$sql = "UPDATE location_product SET productQty='".$proQty."' WHERE productId='".$proid."' and productLocation='".$location."'";
$stmt = $conn->prepare($sql);
// execute the query
$stmt->execute();
echo "Record updated successfully!";
//$data["secion"]=$stmt. " this ";
if ( !$stmt) {
$data["match"]=false;
} else {
//echo "Error updating record: " . $conn->error;
echo "Record updated successfully!";
$data["match"]=true;
}
echo json_encode($data);
} catch (Exception $e) {
$data["match"]=false;
echo json_encode($data);
}
}
if (@$_POST['pettyCash']) {
$pettyCashIn=$_POST['pettyCash'];
$comment= $_POST['comment'];
try {
$sql = "INSERT INTO `customer_service_experts`.`shift` ( `empId`, `pettyCashIn`, `note`) VALUES ( '$empId', '$pettyCashIn', '$comment')";
$stmt = $conn->prepare($sql);
// execute the query
$stmt->execute();
if ( !$stmt) {
$data["match"]=false;
} else {
echo "Record updated successfully!";
$data["match"]=true;
}
echo json_encode($data);
} catch (Exception $e) {
$data["match"]=false;
echo json_encode($data);
}
}
?>
当我执行没有contentType它是真的,但它失败了,给我数据错误(我在函数上使用的警报失败),但如果我使用contentType它去功能.done
和槽,但查询不执行。
你还需要stringify
你发送的数据,像这样
data: JSON.stringify(prodData1),
你可以创建一个辅助函数,你可以在任何地方使用它来做JSON POST
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",
在request payload
中发送数据,但如果你省略它/从ajax中删除它,那么默认情况下jquery ajax的contentType是application/x-www-form-urlencoded; charset=UTF-8
。
让我们看一下它们:
使用Content-Type: application/json
的请求可能看起来像这样:
POST /some-path HTTP/1.1
Content-Type: application/json
{ "foo" : "bar", "name" : "John" }
如果你用method="POST"
和Content-Type: application/x-www-form-urlencoded
提交一个html表单(这是默认的jquery ajax)或Content-Type: multipart/form-data
你的请求可能看起来像这样:
POST /some-path HTTP/1.1
Content-Type: application/x-www-form-urlencoded
foo=bar&name=John
所以如果你发送的数据在请求有效载荷可以通过Content-Type: application/json
发送,你只是不能采取这些张贴值与php超全局变量如$_POST
。
你需要自己用file_get_contents('php://input')
获取原始格式
首先删除content-type选项。让jQuery使用默认值。第二,
改变:
var pLocal= document.getElementById(productId).value;
var prodData = 'pLocal=' + pLocal+ '&proId='+productId;
:
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 };