普通网友 2013-07-28 06:54
浏览 68
已采纳

解析$ get参数时,jquery请求的JSON解析失败

Jquery has is working before the following line is changed

data: "ID=1",

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

from

data: "",

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

my ajax file

<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.
 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.
' + jqXHR.responseText);
                }
            }
        });
    });
    </script>

php file test.php (same dir with ajax file)

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

How to make this work with $_GET["ID"], I need to do action according to the ID which is variable according to the product user chosen


and I've read through manual, long ago however manual is ain't easy for a newcomer you know. "You should read manual, idiot" is the worst answer I've ever heard and I can answer every question I don't know with this sentence. @tereško

further changed to following line but still ain't working,

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

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

2条回答 默认 最新

  • duangua5308 2013-07-28 07:06
    关注

    You are using POST instead of GET . On test.php you need to change your php code to

    <?php
     $ID=$_POST["ID"];
     $array=array("$ID",'B',"C");
     echo json_encode($array);
     ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器