使用Ajax将数据传递给php

时间:2022-12-01 21:33:44

Trying to make an ajax call work.

试图让ajax调用工作。

jQuery Code

<script>
jQuery( document ).ready(function() {
jQuery.ajax({
    url: "/handler/?Action=Variable",
    data: { 
        "Action": "Variable"
    },
    cache: false,
    type: "GET",
    success: function(response) {
        alert(response);
    },
    error: function(xhr) {
        alert(xhr);
    }
});
}
</script>

PHP Code:

<?php
if($_GET['Action'] == 'Variable') {echo "done";}
?>

echo "done"; is never being called. I have tried several different ways of doing an Ajax call and none of them are giving me a desired response.

回声“完成”;永远不会被召唤。我尝试了几种不同的Ajax调用方式,但没有一种方法能给我一个理想的响应。

If anyone could point me in the right direction that would be greatly appreciated.

如果有人能指出我正确的方向,将不胜感激。

Thanks, Luke.

EDIT running print_r($_GET); returns

编辑运行print_r($ _ GET);回报

Array
(
    [Action] => Variable
    [_] => 1450086386289
)   

1 个解决方案

#1


0  

<script>         
    $.ajax({
        var name='myname'; //exampls this is your variable from ajax
        type:'POST',
        url: '../page.php, //the page where you get the  variable
        data:'name=' + name;
        success:function(data){}
        });
</script>

And to GET the variable you pass from AJAX

并获取从AJAX传递的变量

<?php    
    if (isset($_POST['name'])){
        $name =$_POST['name'];

    }
?>

#1


0  

<script>         
    $.ajax({
        var name='myname'; //exampls this is your variable from ajax
        type:'POST',
        url: '../page.php, //the page where you get the  variable
        data:'name=' + name;
        success:function(data){}
        });
</script>

And to GET the variable you pass from AJAX

并获取从AJAX传递的变量

<?php    
    if (isset($_POST['name'])){
        $name =$_POST['name'];

    }
?>