JSON没有通过AJAX POST传递

时间:2022-04-03 21:30:25

I am trying to pass some JSON via AJAX to a php script, here is my javascript:

我试图通过AJAX将一些JSON传递给php脚本,这是我的javascript:

jQuery(document).ready(function($) {
    /**
     * AJAX add to cart
     */
    $( ".single_add_to_cart_button" ).each(function() {

   var el = $(this);

    el.click(function(e) {
            var product_data = $("#jsonVariations").val();

            e.preventDefault();

            $(this).text('Adding to cart');

            // try ajax

            $.ajax({
                url: myAjax.ajaxurl,
                type: "POST",
                data: {
                    action: 'add_bv',
                    product_data: product_data,
                },
                dataType: "json",
                //contentType: "application/json",
                success: function (result) {
                   el.text("Added to cart"); 
                },
                error: function (xhr, ajaxOptions, thrownError) {
                    el.text("Not added to cart"); 
                    //alert(xhr.status);
                    alert(thrownError);
                }
            });


        return false;

    });

});

});

The JSON is valid (tested via jsonlint) and is somehting like this:

JSON是有效的(通过jsonlint测试)并且有点像这样:

`[{"variationQty":5,"variationID":"50","variationSize":"2xl","variationColour":"grey"},{"variationQty":10,"variationID":"51","variationSize":"2xl","variationColour":"navy"}]

My php script is:

我的PHP脚本是:

$product_data = $_POST['product_data'];
    $product_data = json_decode($product_data, true);

    foreach ($product_data as $product) {
            $product_qty = intval( $product->variationQty );
            $product_id  = 24;
            $product_variation_id = $product->variationID;
            $product_variation = array(
                'colour'    => $product->variationColour,
                'size'      => $product->variationSize,
            );
            WC()->cart->add_to_cart( $product_id, $product_qty, $product_variation_id, $product_variation );    
    }

I have trialed setting the $product_data variable manually to the json and it works perfectly, for some reason it wont pass the JSON properly. I have also tried using JSON stringify and set the content type, when this happens I get the AJAX success function but the php script doesn't seem to execute.

我已经尝试将$ product_data变量手动设置为json并且它完美地工作,由于某种原因它不能正确地传递JSON。我也尝试过使用JSON stringify并设置内容类型,当发生这种情况时,我得到了AJAX成功函数但是php脚本似乎没有执行。

4 个解决方案

#1


0  

Element IDs should be unique within the entire document.

元素ID在整个文档中应该是唯一的。

is there is only one div with jsonVariations ID

是否只有一个带有jsonVariations ID的div

#2


0  

I guess this is the : json_decode($product_data, true) the return array ,not an object. It can be json_decode($product_data) or json_decode($product_data, false). Hope I can help you.

我猜这是:json_decode($ product_data,true)返回数组,而不是对象。它可以是json_decode($ product_data)或json_decode($ product_data,false)。希望我能帮助你。

#3


0  

My Mistake I have not fully understand the Problem.

我的错误我还没有完全理解这个问题。

In POSTING json encoded data using ajax, when it reaches to the server it is already converted to a $_POST[...] array

在使用ajax POST POST json编码数据时,当它到达服务器时,它已经转换为$ _POST [...]数组

your $_POST would be:

你的$ _POST将是:

$_POST:
array(
    'action' => 'add_bv',
    'product_data' => array(...the content of product_data...)
);

In your case, I think you dont have to decode the posted json data because you just have to use it as is.

在您的情况下,我认为您不必解码已发布的json数据,因为您只需按原样使用它。

$product_data = $_POST['product_data']; //Correct, allready an array

$product_data = json_decode($product_data, true); //Not needed

#4


0  

Thanks to @vsogrimen I determined the problem was on the php side. For some reason the JSON object had quotes escaped. I updated my code to strip these slashes and it works perfect. Here is my final code:

感谢@vsogrimen我确定问题出在php端。由于某种原因,JSON对象有引号转义。我更新了我的代码来剥离这些斜杠,它完美无缺。这是我的最终代码:

function prefix_ajax_add_bv() {


$product_data = stripslashes($_POST['product_data']);   
$product_data = json_decode($product_data, false);


foreach ($product_data as $product) {

        $product_qty = intval( $product->variationQty );
        $product_id  = 24;
        $product_variation_id = $product->variationID;
        $product_variation = array(
            'colour'    => $product->variationColour,
            'size'      => $product->variationSize,
        );
        WC()->cart->add_to_cart( $product_id, $product_qty, $product_variation_id, $product_variation, array('_my_data','000000000000000000000000000000') );    
}

}

#1


0  

Element IDs should be unique within the entire document.

元素ID在整个文档中应该是唯一的。

is there is only one div with jsonVariations ID

是否只有一个带有jsonVariations ID的div

#2


0  

I guess this is the : json_decode($product_data, true) the return array ,not an object. It can be json_decode($product_data) or json_decode($product_data, false). Hope I can help you.

我猜这是:json_decode($ product_data,true)返回数组,而不是对象。它可以是json_decode($ product_data)或json_decode($ product_data,false)。希望我能帮助你。

#3


0  

My Mistake I have not fully understand the Problem.

我的错误我还没有完全理解这个问题。

In POSTING json encoded data using ajax, when it reaches to the server it is already converted to a $_POST[...] array

在使用ajax POST POST json编码数据时,当它到达服务器时,它已经转换为$ _POST [...]数组

your $_POST would be:

你的$ _POST将是:

$_POST:
array(
    'action' => 'add_bv',
    'product_data' => array(...the content of product_data...)
);

In your case, I think you dont have to decode the posted json data because you just have to use it as is.

在您的情况下,我认为您不必解码已发布的json数据,因为您只需按原样使用它。

$product_data = $_POST['product_data']; //Correct, allready an array

$product_data = json_decode($product_data, true); //Not needed

#4


0  

Thanks to @vsogrimen I determined the problem was on the php side. For some reason the JSON object had quotes escaped. I updated my code to strip these slashes and it works perfect. Here is my final code:

感谢@vsogrimen我确定问题出在php端。由于某种原因,JSON对象有引号转义。我更新了我的代码来剥离这些斜杠,它完美无缺。这是我的最终代码:

function prefix_ajax_add_bv() {


$product_data = stripslashes($_POST['product_data']);   
$product_data = json_decode($product_data, false);


foreach ($product_data as $product) {

        $product_qty = intval( $product->variationQty );
        $product_id  = 24;
        $product_variation_id = $product->variationID;
        $product_variation = array(
            'colour'    => $product->variationColour,
            'size'      => $product->variationSize,
        );
        WC()->cart->add_to_cart( $product_id, $product_qty, $product_variation_id, $product_variation, array('_my_data','000000000000000000000000000000') );    
}

}