为了账号安全,请及时绑定邮箱和手机立即绑定

AJAX Post 请求仅适用于 Web 控制台(预览版)

AJAX Post 请求仅适用于 Web 控制台(预览版)

PHP
心有法竹 2021-09-18 16:11:05
我有一个非常简单的 ajax 请求来“发送”用户在网站上单击的元素的 ID。该脚本仅适用于 Web 控制台(在网络 -> 预览部分)。这发生在每个浏览器中。这是代码:AJAX 请求<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script><script>    $('.point1, .point2, .point3, .point4, .point5, .point6, .point7').click(function(event) {        var itemid = event.target.id;           $.ajax({           type: 'post',           //url: "index.php",           data: {'itemid' : itemid},            cache : false,           async : true,           dataType: 'html',           success: function(data) {                      alert('success');                },           failure: function(data) {                    alert('failure');           }              });    });    </script>PHP函数    <?php       if(isset($_POST['itemid'])){        $itemid = $_POST['itemid'];        echo "success";        $itemid = (int)$itemid;        echo $itemid;    } else{    echo "failure";}    ?>你能帮我解决这个问题吗?只需添加图像即可让您更好地理解。
查看完整描述

2 回答

?
守着一只汪

TA贡献1872条经验 获得超3个赞

这是对你有用的东西。如果它不起作用,那么请分享您的整个代码以及来自 PHP 的响应。


// jQuery AJAX call should be something like this

$.ajax({

    url: 'index.php',

    data: {

        "itemid": itemid

    },

    type: "post",

    dataType: "json",

    success: function(json) {

        if(json.success) {

            alert("Item ID is " + json.itemid);

        } else {

            alert("Item ID is " + json.itemid);

        }

    },

    error: function(jqXHR, textStatus, errorThrown) {

        alert("Error :: " + textStatus + " :: " + errorThrown);

    }

});



// PHP code can be like this

if(isset($_POST['itemid'])){

    $itemid = $_POST['itemid'];

    echo json_encode(['success' => true, 'itemid' => $itemid]);

} else {

    echo json_encode(['success' => false, 'itemid' => 'Not available']);

}


查看完整回答
反对 回复 2021-09-18
  • 2 回答
  • 0 关注
  • 125 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信