简体   繁体   English

通过AJAX将对象传递给PHP

[英]Pass object to PHP through AJAX

I was wondering if it is possible to pass an array to a php function using the jQuery AJAX function. 我想知道是否可以使用jQuery AJAX函数将数组传递给php函数。 I have the following as my javascript 我有以下作为我的JavaScript

arr_data = {
    field01: "data 01",
    field02: "data 02",
    field03: "data 03",
    field04: "data 04"
}

$.ajax({
    url: "scripts/php/phpfunc.php",
    type: "GET",
    dataType: "json",
    data: {
        'action': "exec_find",
        'field01': arr_data["field01"],
        'field02': arr_data["field02"],
        'field03': arr_data["field03"],
        'field04': arr_data["field04"]
    },
    success: function(result) {
        // continue program
    },
    error: function(log) {
        // handle error
    }
});

When I try to do the following though 当我尝试执行以下操作时

arr_data = {
    field01: "data 01",
    field02: "data 02",
    field03: "data 03",
    field04: "data 04"
}

$.ajax({
    url: "scripts/php/phpfunc.php",
    type: "GET",
    dataType: "json",
    data: {
        'action': "exec_find",
        'data': arr_data
    },
    success: function(result) {
        // continue program
    },
    error: function(log) {
        // handle error
    }
});

I receive it in the PHP as "Array". 我在PHP中将其作为“数组”接收。 How can I correctly send the object so that it is usable by the PHP function? 如何正确发送对象,以便PHP函数可以使用它?

Please try to pass the array in the format of json. 请尝试以json格式传递数组。 Then use the get the json in your php and access the json array. 然后在您的php中使用get json并访问json数组。

<script>

arr_data = {
    field01: "data 01",
    field02: "data 02",
    field03: "data 03",
    field04: "data 04"
}
var  myJsonString= JSON.stringify(arr_data);
$.ajax({
    url: "scripts/php/phpfunc.php",
    type: "GET",
    dataType: "json",
    data: {
        'action': "exec_find",
        'data': myJsonString
    },
    success: function(result) {
        // continue program
    },
    error: function(log) {
        // handle error
    }
});
</script>

this is your java script. 这是您的Java脚本。 and below is the php 下面是PHP

$dataJson=json_decode($_GET['data']);

here you can get the json array and loop through it and do what ever you want. 在这里,您可以获取json数组并遍历它,然后执行您想做的任何事情。

Please have try at this. 请尝试一下。 This is working in my case. 就我而言,这是可行的。

from the second ajax you can access the data based on the property names like: $_GET['data']['field01'] 从第二个ajax中,您可以基于属性名称访问数据,例如: $_GET['data']['field01']

$_GET['data'] is the js object converted in php in a associative array $_GET['data']是在php中以关联数组转换的js对象

try this : 尝试这个 :

$.ajax({
    url: "scripts/php/phpfunc.php",
    method: "POST",
    dataType: "json",
    data: {
        'action': "exec_find",
        'data': arr_data.serialize()
    },

serialize() 连载()

http://api.jquery.com/serialize/ http://api.jquery.com/serialize/

convert your array in string 将您的数组转换为字符串

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM