简单的JQuery POST甚至没有工作

时间:2013-04-07 16:41:09

标签: php jquery post

好的,所以我对JQuery很陌生,但无论如何,这似乎是一件非常简单的事情。

我正在开发各种各样的“购物车”,其中的项目存储在本地,然后通过jquery.post发送到邮件脚本。

现在,由于某些原因,我的代码没有做任何事情,我不知道为什么,因为它的代码简单明了。我已经将我的cart变量替换为简单文本来测试它,因为我认为我的数据可能是真正的罪魁祸首。

$(".order-cart").click(function() {
    $current_cart = "test";
    $.post('../thank-you.php', {cart_items:$current_cart}, function (response) {
        alert(response);
    });
    return false;
});

我已经测试了按钮,工作正常。但它仍然没有做任何事情......为什么这不起作用?我错过了什么?我也在服务器上测试了它,无论是WAMP还是在线。

PHP就是这样:

<?php
$cart_items = $_POST["cart_items"];
echo $cart_items;
?>

非常感谢!

Edit:
After playing around with it a bit, I noticed the following error in the console (using Chrome)
XMLHttpRequest cannot load file:///C:/Design/Assault%20Ou%20Studios/Clients/Client%20Work/2Dive4%20Scuba/Website/thank-you.php. Origin null is not allowed by Access-Control-Allow-Origin. 

任何人都知道这意味着什么?

3 个答案:

答案 0 :(得分:0)

尝试初始化变量,如下所示:

$(".order-cart").click(function() {
    var current_cart = "test";
    $.post('../thank-you.php', {cart_items: current_cart}, function (response) {
        alert(response);
    });
    return false;
});

答案 1 :(得分:0)

对我有用!
把它放在新文件中并测试它

<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
<input type='button' class='order-cart' value='click me'>
<script>
$(".order-cart").click(function() {
    $current_cart = "test";
    $.post('../thank-you.php', {cart_items:$current_cart}, function (response) {
        alert(response);
    });
    return false;
});
</script>

您还确定您的“thank-you.php”文件位置是否正确?

答案 2 :(得分:0)

你的脚本是正确的!!!!
您的“thank-you.php”文件位置不正确!!!