我似乎无法让这个对象通过 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']);
关于javascript - 通过 jQuery ajax 将 jQuery 构造的数组发送到 PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12576263/