通过AJAX将对象传递给PHP [英] Pass object to PHP through AJAX

查看:81
本文介绍了通过AJAX将对象传递给PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以使用jQuery AJAX函数将数组传递给php函数.我将以下内容用作我的javascript

I was wondering if it is possible to pass an array to a php function using the jQuery AJAX function. I have the following as my 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
    }
});

我在PHP中将其作为数组"接收.如何正确发送对象,以便PHP函数可以使用它?

I receive it in the PHP as "Array". How can I correctly send the object so that it is usable by the PHP function?

推荐答案

在第二个ajax中,您可以基于属性名称访问数据,例如:$_GET['data']['field01']

from the second ajax you can access the data based on the property names like: $_GET['data']['field01']

$_GET['data']是在php中以关联数组转换的js对象

$_GET['data'] is the js object converted in php in a associative array

这篇关于通过AJAX将对象传递给PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆