我正在尝试将一个简单的字符串化 int 数组传递给 json 表达式并通过 ajax 发送。从客户端到服务器的请求 url 和标头对我来说都很好,但是在 Cake 中,请求->查询 vardump 出于某种原因为空。
来自ajax的标题:
Request URL:http://localhost/cakephp-2.2.5/paragraphs/sortPublished.json?[%221%22,%223%22,%222%22]
Request Method:GET
Status Code:200 OK
Request Headersview source
Accept:application/json, text/javascript, */*; q=0.01
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:fi-FI,fi;q=0.8,en-US;q=0.6,en;q=0.4
Connection:keep-alive
Content-Type:application/json; charset=utf-8
Cookie:CAKEPHP=gu46lhu6lohe1hnr4vmeve23g1
DNT:1
Host:localhost
Referer:http://localhost/cakephp-2.2.5/paragraphs/edit/1
User-Agent:Mozilla/5.0 (Windows NT 6.0) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.64 Safari/537.31
X-Requested-With:XMLHttpRequest
Query String Parametersview sourceview URL encoded
["1","3","2"]:
Response Headersview source
Connection:Keep-Alive
Content-Length:425
Content-Type:application/json; charset=UTF-8
Date:Mon, 13 May 2013 09:11:14 GMT
Keep-Alive:timeout=5, max=98
Server:Apache/2.4.3 (Win32) OpenSSL/1.0.1c PHP/5.4.7
X-Powered-By:PHP/5.4.7
和 Javascript
$(document).ready(function(){
$("#button").click(function() {
var sortedParagraphs = $( "#sortable1" ).sortable('toArray');
var key_value_pairs = [];
var i = 0;
$.each( sortedParagraphs, function(index, value) {
key_value_pairs[index] = value;
});
$.ajax({
url: define1,
data: JSON.stringify(sortedParagraphs),
dataType: "json",
contentType: 'application/json; charset=utf-8',
success: function(data) {
alert(data);
}
});
});
});
对于我的业余爱好者来说,显然 JSON 格式正确,并且我看不到 url 有任何问题,但 request->query 仍然会产生:
array(0) {
}
我一直在努力解决这个小问题,因为它感觉永远存在,任何帮助将不胜感激。