[英]send php array through jquery ajax with index and recieve the array value
[英]Send jQuery constructed array through jQuery ajax to PHP
我似乎无法让这个对象通过jQuery的ajax传递给我的PHP文件。
$("body").on("submit", ".upload-results-form", function(){
var photosJSON = [];
$(".upload-result-wrapper .upload-results .photos li").each(function(){
var photoID = $(this).attr("rel");
var description = $(".photo-upload-description", this).val();
var source = $("img", this).attr("src");
photosJSON.push({photoID: photoID, description: description, source: source});
});
var jsonData = JSON.stringify(photosJSON);
$.ajax({
type: "POST",
url: "ajax/add/albums/photos_publish.php",
data: "photosJSON="+jsonData,
cache: false,
success: function(html){
alert(html);
}
});
});
我的jsonData看起来像这样:
[{"photoID":"47","description":"","source":"photos/50611a8725cca_224.jpg"},
{"photoID":"48","description":"","source":"photos/50611a8764881_224.jpg"},
{"photoID":"49","description":"","source":"photos/50611a87aa508_224.jpg"},
{"photoID":"50","description":"","source":"photos/50611a88dd34b_224.jpg"}]
和我的php文件:
$photosJSON = json_decode($_POST['photosJSON']);
echo $photosJSON['photoID'];
但是没有返回任何东西,似乎没有任何东西被发送到php。
您必须将javascript对象转换为JSON:
data: {photoDescriptions: JSON.stringify(photoDescriptions)},
然后,在您的PHP代码中:
$photoDescriptions = json_decode($_POST['photoDescriptions']);
首先要做的事情是:AJAX调用的“data”属性需要成为一个对象,就像timidboy所说的那样。
两次快速健全检查:
console.log(photosJSON);
在.each()结束后,和
var_dump($_POST);
在PHP页面的某个地方。 我怀疑,假设你遵循了timidboy的更正,问题不在你的AJAX中。 例如,您确定每个()迭代的标识符实际上与页面上的DOM元素匹配吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.