AJAX jquery json将数组发送到php [英] AJAX jquery json sending array to php

查看:110
本文介绍了AJAX jquery json将数组发送到php的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过AJAX $ .post向php发送一个关联数组。这是我的代码:

I'm trying to send a associative array via AJAX $.post to php. Here's my code:

var request = {
        action: "add",
        requestor: req_id,
        ...
    }

    var reqDetails = $("#request_details").val();

    switch(reqDetails){
        case 1: 
            request[note] = $("#note").val();
            break;

        ...

    }

    if(oldRequest()){
        request[previousID] = $("old_id").val();
    }

    $('#req_button').toggleClass('active');
    $.post("scripts/add_request.php", {
        request_arr: JSON.stringify(request)
        }, function(data){
            console.log(data);
            $('#req_button').toggleClass('active');
    }, 'json');

我只是想在我的php脚本中读取收到的数据:

And i'm simply trying to read the received data in my php script:

echo json_decode($_POST["request_arr"]);

但它不起作用。我是js的新手,我无法弄清楚我做错了什么。

But it's not working. I'm a newbie to js, I can't figure out what I'm doing wrong.

推荐答案

检查以下链接您的参考

从JavaScript / jQuery向PHP发送数组

$.ajax({
    type: "POST",
    url: "parse_array.php",
    data:{ array : JSON.stringify(array) },
    dataType: "json",
    success: function(data) {
        alert(data.reply);
    }
});



第2步



你的php文件看起来像这样:

Step 2

You php file looks like this:

<?php 



  $array = json_decode($_POST['array']);
    print_r($array); //for debugging purposes only

     $response = array();
     if(isset($array[$blah])) 
        $response['reply']="Success";
    else 
        $response['reply']="Failure";

   echo json_encode($response);



第3步



成功函数

Step 3

The success function

success: function(data) {
        console.log(data.reply);
        alert(data.reply);
    }

这篇关于AJAX jquery json将数组发送到php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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