簡體   English   中英

Ajax不會傳遞給$ _POST

[英]Ajax won't pass to $_POST

將ajax傳遞到$_POST遇到麻煩,它總是返回false。

到目前為止我嘗試過的是:

  • 使用contentType: 'application/json; charset=UTF-8' 'application/json; charset=UTF-8'
  • 為ajax創建函數並添加到jQuery onclick
  • 在Javascript中刪除了dataType, console.log: Object {readyState: 0, responseText: "", status: 0, statusText: "error"}
  • 使用Javascript中的dataType, 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);
?>

在控制台上,它將顯示"Error"

在cart.PHP上,它將顯示"Failed to hold"

我所知道的是ajax運行不正常,我不知道如何解決它。

Ajax的最佳解決方案,但由於某種原因無法發布

不知道為什么,但是對我有用。

創建一個函數:

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")
        }
    });     
}

將其添加到javascript中:

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

您沒有在任何要發送數據數據的地方聲明購物車:{'jCart':JSON.stringify(cart)}

嘗試像這樣更改代碼:

        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

   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