3

由于某种原因,我的 php 无法读取 javascript 创建的 json 对象。

这是打印到控制台的。

{
    "product": {
        "shipsTo": "Please choose …",
        "accountExec": "Please choose …",
        "product": "Please choose …",
        "productSize": "Please choose …",
        "qty": "",
        "comments": ""
    },
    "billing": {
        "billingName": "",
        "billingAttn": "",
        "billingAddress1": "",
        "billingAddress2": "",
        "billingCity": "",
        "billingState": "",
        "billingZip": "",
        "billingPhone": "",
        "billingFax": "",
        "billingEmail": "asdf@asdf.com"
    }
}

我唯一填写的是billingEmail.

我通过从表单中收集大约 15 个字段并将它们放入 javascript 对象中来获取这些值

$(document).ready(function(){
    $('#CBS_submit').on("click", function(){

    //validate


    //collect information
    var orderInfo = {};
    orderInfo.product = {};
    orderInfo.billing = {};
    orderInfo.product.shipsTo     = $('#CBS_ship_to_select option:selected').val();
    orderInfo.product.accountExec = $('#CBS_account_execs_select option:selected').val();
    orderInfo.product.product     = $('#CBS_product_select option:selected').val();
    orderInfo.product.productSize = $('#CBS_product_size_select option:selected').val();
    orderInfo.product.qty         = $('#CBS_qty').val();
    orderInfo.product.comments    = $('#CBS_comments').val();
    //capture textarea information if other is selected for
    //shipsTo, product, productSize
    if ( get_selected_id ('CBS_ship_to_select') == 4 ) {
        orderInfo.product.shipsTo = $('#other_ship_to_text').val();
    }
    if ( get_selected_id ('CBS_product_select') == 4 ) {
        orderInfo.product.product = $('#other_product_text').val();
    }
    if ( get_selected_id ('CBS_product_size_select') == 4 ) {
        orderInfo.product.productSize = $('#other_product_size_text').val();
    }


    orderInfo.billing.billingName = $('#CBS_billing_name').val();
    orderInfo.billing.billingAttn = $('#CBS_billing_attn').val();
    orderInfo.billing.billingAddress1 = $('#CBS_billing_address1').val();
    orderInfo.billing.billingAddress2 = $('#CBS_billing_address2').val();
    orderInfo.billing.billingCity = $('#CBS_billing_city').val();
    orderInfo.billing.billingState = $('#CBS_billing_state').val();
    orderInfo.billing.billingZip = $('#CBS_billing_zip').val();
    orderInfo.billing.billingPhone = $('#CBS_billing_phone').val();
    orderInfo.billing.billingFax = $('#CBS_billing_fax').val();
    orderInfo.billing.billingEmail = $('#CBS_billing_email').val(); 


    var orderInfoJSON = JSON.stringify(orderInfo);
    console.log(orderInfoJSON);
        $.ajax({
           type: "POST",
           url: "queries/submit_order.php",
           data: "order_info=" + orderInfoJSON,
           dataType: "json",
           success: function (data) {
           console.log('test');


            }
        });
    });

});

然后在 php 页面上请求它并开始将它放入 html

<?php
$order_info = $_REQUEST['order_info'];

$order_info = json_decode($order_info, TRUE);
$msg = <<<EOD

<table style='width:600px; font-family:\"Helvetica\",\"Arial\", sans-serif; margin:15px' border="0">

访问变量,如

{$order_info['product']['product']}

但由于某种原因,我没有收到任何提交。即使当我复制/粘贴对象时

$order_info = '{"product":{"shipsTo":"Please choose …","accountExec":"Please choose …","product":"Please choose …","productSize":"Please choose …","qty":"","comments":""},"billing":{"billingName":"","billingAttn":"","billingAddress1":"","billingAddress2":"","billingCity":"","billingState":"","billingZip":"","billingPhone":"","billingFax":"","billingEmail":"asdf@asdf.com"}}'; 有用

我究竟做错了什么?太感谢了!

4

1 回答 1

1

如果它包含特殊字符,则将其作为字符串发送可能会搞砸。尝试改用此方法:

$.ajax({
    type: "POST",
    url: "queries/submit_order.php",
    data: {order_info: orderInfoJSON},
    dataType: "json",
    success: function (data) {
        console.log('test');
    }
});
于 2012-08-29T19:58:46.533 回答