weixin_33695450 2016-10-09 07:44 采纳率: 0%
浏览 26

Ajax不会传递给$ _POST

Having troubles passing the ajax to $_POST, it is always returning false.

What I have tried so far:

  • Using contentType: 'application/json; charset=UTF-8',
  • Creating a function for ajax and adding to jQuery onclick
  • Removed dataType in Javascript, console.log: Object {readyState: 0, responseText: "", status: 0, statusText: "error"}
  • With dataType in Javascript, Object {readyState: 4, responseText: "↵ ↵Failed to hold<br>", status: 200, statusText: "OK"}

Javascript:

        $(document).ready(function(){
            $("#showcart").click(function(event){
                event.preventDefault();
                $.ajax({
                    data: {'jCart':JSON.stringify(cart)},
                    type: 'POST',
                    dataType: 'json',   
                    url: 'storecart.php',
                    contentType:'application/json; charset=UTF-8',
                    success: function(data){
                        console.log("Success")
                    },
                    error: errorFunction
                });

            }); 
        });

function errorFunction(){
    console.log("Error");
}

Storecart.php

<?php
    if(isset($_POST['jCart'])){
        $decode = json_decode($_POST['jCart']);
        $_SESSION['receive'] = $decode;
        $product = $_SESSION['receive'];
    }
    else{
        echo "Failed to hold<br>";
    }
?>

Cart.php

<?php 
    session_store();
    include(Storecart.php);
?>

On the console, it will say "Error".

On the cart.PHP, it will say "Failed to hold".

What I know is that ajax is not running properly, I don't know how to fix it.

ALMOST SOLUTION for ajax, but it can't post for some reason

Don't know why but it worked for me.

Create a function:

function showcart(){
    var jData = JSON.stringify(cart);
    $.ajax({
        url:"storecart.php",
        type:"post",
        data: 'datastring=' + jData,
        datatype: "json",
        success: function(data){
            console.log("SUCCESS")
            console.log(jData);
        },
        error: function(data){
            console.log("REDO")
        }
    });     
}

Add it to the javascript:

 $(document).ready(function(){
    $("#showcart").click(function(event){
        event.preventDefault();
        showcart();
        }); 
    });
  • 写回答

2条回答 默认 最新

  • elliott.david 2016-10-09 07:49
    关注

    You did not declare cart anywhere which is sending in data data: {'jCart':JSON.stringify(cart)}

    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!