-1

在更改以下行之前,Jquery has 正在工作

data: "ID=1",

$ID=$_GET["ID"]
$array=array("$ID",'B',"C");

data: "",

$array=array('A','B',"C");

我的ajax文件

<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>

    <button class="ajax_action">click</button>
    <script>
    $('button.ajax_action').on('click', function (e) {
        e.preventDefault();
        var target = $(this);
        $.ajax({
            url: "test.php",
            data: "ID=1",
            type: 'POST', 
            dataType: 'json',
            success: function (data) {
                var name=data[0];
                target.html(name);
                target.attr('disabled', 'disabled')
            },
               error: function(jqXHR, exception) {
                if (jqXHR.status === 0) {
                    alert('Not connect.\n Verify Network.');
                } else if (jqXHR.status == 404) {
                    alert('Requested page not found. [404]');
                } else if (jqXHR.status == 500) {
                    alert('Internal Server Error [500].');
                } else if (exception === 'parsererror') {
                    alert('Requested JSON parse failed.');
                } else if (exception === 'timeout') {
                    alert('Time out error.');
                } else if (exception === 'abort') {
                    alert('Ajax request aborted.');
                } else {
                    alert('Uncaught Error.\n' + jqXHR.responseText);
                }
            }
        });
    });
    </script>

php 文件 test.php(与 ajax 文件相同的目录)

<?php
    $ID=$_GET["ID"]
    $array=array("$ID",'B',"C");
    echo json_encode($array);
    ?>

如何使用 $_GET["ID"] 进行这项工作,我需要根据 ID 执行操作,该 ID 根据选择的产品用户而变化


而且我很久以前就通读了手册,但是对于您认识的新手来说,手册并不容易。“你应该阅读手册,白痴”是我听过的最糟糕的答案,我可以用这句话回答我不知道的每一个问题。@tereško

进一步更改为以下行,但仍然无法正常工作,

    data: {'ID':1},
    type: 'POST', 

<?php
$ID=$_POST["ID"]
$array=array("$ID",'B',"C");
echo json_encode($array);
?>
4

2 回答 2

0

您正在使用 POST 而不是 GET 。在 test.php 上,您需要将 php 代码更改为

<?php
 $ID=$_POST["ID"];
 $array=array("$ID",'B',"C");
 echo json_encode($array);
 ?>
于 2013-07-28T07:06:31.170 回答
0

将ajax调用中的类型更改为type: 'GET'

于 2013-07-28T07:41:15.060 回答