使用jQuery ajax发送到服务器的formData对象在成功时返回空数组

时间:2015-10-15 06:54:23

标签: javascript php jquery ajax windows-server-2012

标题基本上都说明了。

我正在收集一些数据并将其附加到formData对象中,以便将其发布到我的PHP文件中并处理其余部分。

我的Ajax功能:

a

updateObj包含我附加到formData Object的所有内容。此变量的控制台日志返回所有内容。所以问题必须在ajax POST中正确。

更新 console.log(updateObj)的内容:

save.addEventListener("click", function(e){
    e.preventDefault();
    getAllContents();
    console.log(updateObj);

    updateObj = JSON.stringify(updateObj);
    console.log(updateObj);

    $.ajax({
        url: "test.php",
        type: "POST",
        data: updateObj,
        success: function(response){
            console.log("Success: ", response);
        },
        error: function(response){
            console.log("Error: ", response);
        }
    });
});

我的PHP文件只包含[{"row_id":1,"status":"Anmeldung","ma_name":"AA","datum":"/","fa1":"testname1","fa2":"testname2","limit":"10.000","gruppe_kredit":"/","omv_kdnr":"8124213","sap_kdnr":"/","fos":"/","hga_kdnr":"/"},{"row_id":3,"status":"Anmeldung","ma_name":"AA","datum":"/","fa1":"ame1","fa2":"name2","limit":"12.000","gruppe_kredit":"/","omv_kdnr":"81515616","sap_kdnr":"/","fos":"/","hga_kdnr":"/"},{"row_id":2,"status":"Kunde","ma_name":"AA","datum":"/","fa1":"newname1","fa2":"newname2","limit":"15.323","gruppe_kredit":"/","omv_kdnr":"81515616","sap_kdnr":"/","fos":"/","hga_kdnr":"/"}]

print_r
$_POST

这是我的ajax调用记录的成功函数:

<?php 
   print_r($_POST);
?>

UPDATE2

这就是我的formData对象的填写方式:

success:  Array
(
)

3 个答案:

答案 0 :(得分:2)

问题是你正在尝试发送一个JS对象数组。在这种情况下,正常JSON.stringify(obj)不会起作用,因为单个对象上没有键,因此对象不能正确编码到字符串中,所以在服务器端它不会被解析正确而且无法传递给$_POST。一种解决方案可能是

updateObj = [{"row_id":1,"status":"Anmeldung","ma_name":"AA","datum":"/","fa1":"testname1","fa2":"testname2","limit":"10.000","gruppe_kredit":"/","omv_kdnr":"8124213","sap_kdnr":"/","fos":"/","hga_kdnr":"/"},{"row_id":3,"status":"Anmeldung","ma_name":"AA","datum":"/","fa1":"ame1","fa2":"name2","limit":"12.000","gruppe_kredit":"/","omv_kdnr":"81515616","sap_kdnr":"/","fos":"/","hga_kdnr":"/"},{"row_id":2,"status":"Kunde","ma_name":"AA","datum":"/","fa1":"newname1","fa2":"newname2","limit":"15.323","gruppe_kredit":"/","omv_kdnr":"81515616","sap_kdnr":"/","fos":"/","hga_kdnr":"/"}];
        // window.updateObj =
    console.log(updateObj);
    Obj = {};
    $.each(updateObj,function(x,obj){ Obj[""+x] = obj;});
    // updateObj = JSON.stringify(updateObj);
    updateObj = JSON.stringify(Obj);
    console.log(Obj);

$.ajax({
        url: "//localhost:80/test/",
        type: "POST",
        data: updateObj,
        success: function(response){
            console.log("Success: ", response);
        },
        error: function(response){
            console.log("Error: ", response);
        }
    });

------------------------更新----------------------

你也可以这样做,但我猜想解码服务器大小会有点棘手。

updateObj = [{"row_id":1,"status":"Anmeldung","ma_name":"AA","datum":"/","fa1":"testname1","fa2":"testname2","limit":"10.000","gruppe_kredit":"/","omv_kdnr":"8124213","sap_kdnr":"/","fos":"/","hga_kdnr":"/"},{"row_id":3,"status":"Anmeldung","ma_name":"AA","datum":"/","fa1":"ame1","fa2":"name2","limit":"12.000","gruppe_kredit":"/","omv_kdnr":"81515616","sap_kdnr":"/","fos":"/","hga_kdnr":"/"},{"row_id":2,"status":"Kunde","ma_name":"AA","datum":"/","fa1":"newname1","fa2":"newname2","limit":"15.323","gruppe_kredit":"/","omv_kdnr":"81515616","sap_kdnr":"/","fos":"/","hga_kdnr":"/"}];
    console.log(updateObj);
    Obj = {};
    Obj["updateObj"] = updateObj;
    updateObj = JSON.stringify(Obj);
    $.ajax({
            url: "//localhost:80/test/",
            type: "POST",
            data: updateObj,
            success: function(response){
                console.log("Success: ", response);
            },
            error: function(response){
                console.log("Error: ", response);
            }
        });

<强> ---------- update2.0

updateObj = [{"row_id":1,"status":"Anmeldung","ma_name":"AA","datum":"/","fa1":"testname1","fa2":"testname2","limit":"10.000","gruppe_kredit":"/","omv_kdnr":"8124213","sap_kdnr":"/","fos":"/","hga_kdnr":"/"},{"row_id":3,"status":"Anmeldung","ma_name":"AA","datum":"/","fa1":"ame1","fa2":"name2","limit":"12.000","gruppe_kredit":"/","omv_kdnr":"81515616","sap_kdnr":"/","fos":"/","hga_kdnr":"/"},{"row_id":2,"status":"Kunde","ma_name":"AA","datum":"/","fa1":"newname1","fa2":"newname2","limit":"15.323","gruppe_kredit":"/","omv_kdnr":"81515616","sap_kdnr":"/","fos":"/","hga_kdnr":"/"}];
        // window.updateObj =
    console.log(updateObj);
    Obj = {};


    var Obj = updateObj.reduce(function(o, v, i) {
       o[i] = v;
       return o;
    }, {});
    updateObj = JSON.stringify(Obj);
    console.log(Obj);

$.ajax({
        url: "//localhost:80/test/",
        type: "POST",
        data: updateObj,
        success: function(response){
            console.log("Success: ", response);
        },
        error: function(response){
            console.log("Error: ", response);
        }
    });

答案 1 :(得分:0)

如果您想以json格式获得响应,请尝试使用参数json [编辑]

save.addEventListener("click", function(e){
        e.preventDefault();
        getAllContents();
        console.log(updateObj);

        updateObj = JSON.stringify(updateObj);
        console.log(updateObj);

        $.ajax({
            url: "test.php",
            type: "POST",
            dataType: "json",
            data: ({data:updateObj}),
            success: function(response){
                console.log("Success: ", response);
            },
            error: function(response){
                console.log("Error: ", response);
            }
        });
    });

并在服务器端

  

parse_str($ _ POST [&#39;数据&#39;],$数据);
       echo json_encode($ data);退出;

答案 2 :(得分:0)

试试这个 - 在ajax电话 -

from multiprocessing import Pool

start = time.time()

pool = Pool()

results = pool.map(slow_function, self.job_titles)

for r in results:
   # update your `global` variables here

end = time.time()
print "New time taken for all jobs:", end - start

在Php

   $.ajax({
            url: "test.php",
            type: "POST",
            dataType: "json",
            data: updateObj,
            success: function(response){
                console.log("Success: ", response);
            },
            error: function(response){
                console.log("Error: ", response);
            }