我试图将javascript代码中的POST发送到PHP脚本。
在我看来,数据没有将json数据作为参数发送出错。
以下是我生成de json数据的方法:
query_string_fn = {};
query_string_fn ["cdinterno"] = cdinterno;
o.row.find("input, select").each(function() {
var val = $(this).val();
var id = $(this).attr('name');
query_string_fn [id] = val;
if (id == 'cdfornecedor_new') {
var cmbSelected = $(this)[0];
value_label = cmbSelected.options[cmbSelected.selectedIndex].text;
} else if (id == 'cdtipo_new') {
var tipocmbSelected = $(this)[0];
tipovalue_label = tipocmbSelected.options[tipocmbSelected.selectedIndex].text;
} else {
$(this).val(val);
}
}).end();
if (value_label.length > 0)
o.row[0].innerHTML = value_label;
if (tipovalue_label.length > 0)
o.row[11].innerHTML = tipovalue_label;
editarFN_Post(query_string_fn);
以下是我将数据发送到php的方式。有一些测试评论我已经测试过:
function editarFN_Post(query) {
query["action"] = 2;
var data = query;
$.ajax({
type: "POST",
dataType: "json",
url: "funcoesFN_Fornecedor_Produto_post.php",
//processData: false,
//contentType: "application/json; charset=UTF-8",
//contentType: 'application/json; charset=UTF-8',
//data: data,
//data: data.toString(),
data: JSON.stringify(data),
//data: {data: query},
success: function(rsp) {
alert ("Success!");
alert (rsp);
},
failure: function(rsp) {
alert ("Failed...");
alert (rsp);
}
});
}
这是我检查参数是否已发送的PHP代码:
header("Content-type: application/json; charset=utf-8");
echo "action=" . $_POST["action"] . "<br>";
foreach ($_POST as $key => $value)
echo "Field ".htmlspecialchars($key)." is ".htmlspecialchars($value)."<br>";
var_dump($_POST);
以上测试均未返回数据。
谢谢!