简体   繁体   English

Ajax不会传递给$ _POST

[英]Ajax won't pass to $_POST

Having troubles passing the ajax to $_POST , it is always returning false. 将ajax传递到$_POST遇到麻烦,它总是返回false。

What I have tried so far: 到目前为止我尝试过的是:

  • Using contentType: 'application/json; charset=UTF-8' 使用contentType: 'application/json; charset=UTF-8' 'application/json; charset=UTF-8' , 'application/json; charset=UTF-8'
  • Creating a function for ajax and adding to jQuery onclick 为ajax创建函数并添加到jQuery onclick
  • Removed dataType in Javascript, console.log: Object {readyState: 0, responseText: "", status: 0, statusText: "error"} 在Javascript中删除了dataType, 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中的dataType, Object {readyState: 4, responseText: "↵ ↵Failed to hold<br>", status: 200, statusText: "OK"}

Javascript: 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 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 Cart.php

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

On the console, it will say "Error" . 在控制台上,它将显示"Error"

On the cart.PHP, it will say "Failed to hold" . 在cart.PHP上,它将显示"Failed to hold"

What I know is that ajax is not running properly, I don't know how to fix it. 我所知道的是ajax运行不正常,我不知道如何解决它。

ALMOST SOLUTION for ajax, but it can't post for some reason Ajax的最佳解决方案,但由于某种原因无法发布

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: 将其添加到javascript中:

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

您没有在任何要发送数据数据的地方声明购物车:{'jCart':JSON.stringify(cart)}

Try to change your code like this : 尝试像这样更改代码:

        var dataJcart = {
         "var": "example jcart"
        };
        var dataString = JSON.stringify(dataJcart);

          $(document).ready(function(){
            $("#showcart").click(function(event){
            event.preventDefault();
            $.ajax({
                url: 'storecart.php',
                type: 'POST',
                data:{'jCart': dataString},
                success: function(data){
                    console.log(data);
                },
                error: errorFunction
            });

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

Storecart.php Storecart.php

   if(isset($_POST["jCart"]))
    {
        $decode = json_decode($_POST["jCart"]);
        echo $decode->var;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM