jquery/js与php的通信问题
我正在使用jquery/JS和PHP进行简单的客户机/服务器通信。它可以正常工作,直到数据中包含jquery/js与php的通信问题,php,javascript,jquery,ajax,Php,Javascript,Jquery,Ajax,我正在使用jquery/JS和PHP进行简单的客户机/服务器通信。它可以正常工作,直到数据中包含。 尝试使用以下标题: asdf-wq1-->有效 test1-->有效 bigip1.local-->.”被替换为“\code” 我已经在代码中添加了escape()函数,但结果是一样的 function xy(){ for (var i = 0; i < nodes.length; i++) { var xy = escape(nodes[i].title) +"
。
尝试使用以下标题:
asdf-wq1-->有效
test1-->有效
bigip1.local-->.”被替换为“\code”
escape()
函数,但结果是一样的
function xy(){
for (var i = 0; i < nodes.length; i++) {
var xy = escape(nodes[i].title) +"=" +escape(nodes[i].translate.x + "/" + nodes[i].translate.y);
$.ajax({
url: 'save_layout.php',
data: xy,
dataType: "text",
type: 'post',
success: function(output) {
$("#output").html(output);
},
error: function (response, status, error) {
alert("error" + response.responseText);
}
});
}
}
Firebug输出请求:
POST http /frontend/save_layout.php
200 OK 186ms
jquery....min.js (Zeile 4)
HeaderPostAntwortHTML
Parameterapplication/x-www-form-urlencoded
bigip1.local 470/390
Quelle
bigip1.local=470/390
发布http/frontend/save_layout.php
200 OK 186ms
jquery….min.js(Zeile 4)
HeaderPostAntwortHTML
Parameterapplication/x-www-form-urlencoded
bigip1.local 470/390
奎尔
bigip1.本地=470/390
Firebug输出(响应):
bigip1_本地470/390
正如您所看到的-它似乎被正确地发送到服务器,但在服务器上,只要读取我们的$\u POST
-'。
就突然变成了'
希望有人能在这里帮助我 在javascript代码中,您可以尝试使用
JSON.stringify(values);
然后在php中使用json_decode()
for (var i = 0, l = nodes.length; i < l; i++) {
var data = {};
data[nodes[i].title] = nodes[i].translate.x + "/" + nodes[i].translate.y;
$.ajax({
url: 'save_layout.php',
data: data,
dataType: "text",
type: 'post',
success: function(output) {
$("#output").html(output);
},
error: function (response, status, error) {
alert("error" + response.responseText);
}
});
}
for(变量i=0,l=nodes.length;i
不应手动将
数据
转换为字符串。jQuery就是这样做的。只需将对象而不是字符串传递给Ajax函数
而且你不应该(永远不要!)使用escape()
。此功能已损坏,没有理由使用它。如果出于某种原因必须进行手动URL编码,请使用encodeURIComponent()
function xy(nodes) {
$.each(nodes, function (i, node) {
$.post("save_layout.php", {
title: node.title,
x: node.translate.x,
y: node.translate.y
})
.done(function (output) {
$("#output").html(output);
})
.fail(function (response, status, error) {
alert("error" + response.responseText);
});
});
}
还要注意我对您的代码所做的一些其他更改,以使其在jQuery上下文中更为惯用:
作为参数而不是全局变量传入的事实。这使得功能更加独立节点
- 使用
替换for循环$.each()
- 使用显式的
而不是更通用的$.post()
$.ajax()
- 所有数据都是作为值而不是键传递的事实。对于每个请求,
、title
、x
键都是相同的。这使得服务器端(和客户端)的工作变得更容易y
- 使用
和.done()
回调,这些回调可以附加到.fail()
、.post()
和.get()
,因为它们的本质是承诺。您可以这样做,也可以这样做——这是在jQuery中处理Ajax回调的一种非常方便的方法李>.ajax()
您可能需要考虑将代码重构为对所有对象发出一个Ajax请求,而不是对每个对象发出一个请求。HTTP请求需要时间,最好将它们组合起来。尝试让jQuery进行编码:
var xy={};xy[节点[i].标题]=节点[i].翻译.x+“/”+节点[i].翻译.y代码>这样jQuery将为您字符串化帖子正文。嗨!谢谢你的及时回复,但不幸的是我仍然面临着同样的问题。。。我可以在firebug post数据中看到正确的值“bigip1.local”,但在php中,我仍然会在访问$\u post时获得“bigip1\u local”…嗨!谢谢你的及时回复!我喜欢你的方法,因为它更直截了当-但不幸的是,我仍然面临着同样的问题。。。我添加了window.console.log([nodes[i].title]);在ajax请求之前,它显示了正确的值“bigip1.local”,但在php中,我仍然会在访问$u POST…+1时获得“bigip1_local”,如果这仍然不能解决问题,那么php方面就有问题了。它似乎真的解决了问题-我现在就测试并确认,然后接受解决方案!!太多了!!
for (var i = 0, l = nodes.length; i < l; i++) {
var data = {};
data[nodes[i].title] = nodes[i].translate.x + "/" + nodes[i].translate.y;
$.ajax({
url: 'save_layout.php',
data: data,
dataType: "text",
type: 'post',
success: function(output) {
$("#output").html(output);
},
error: function (response, status, error) {
alert("error" + response.responseText);
}
});
}
function xy(nodes) {
$.each(nodes, function (i, node) {
$.post("save_layout.php", {
title: node.title,
x: node.translate.x,
y: node.translate.y
})
.done(function (output) {
$("#output").html(output);
})
.fail(function (response, status, error) {
alert("error" + response.responseText);
});
});
}