dpdp42233 2014-09-23 12:44
浏览 48
已采纳

jquery ajax数据类型json不能使用php关联数组

i am new to the stackflow and m having some problems while using jquery' json ajax encoded values from an associative array, the jquery script is doing nothing!

its a script that just does nothing but gives you back both username and password back to you via json encode...

here is my code of the index.html file:

<html>
<head>
    <title>test jquery</title>
    <script type="text/javascript" src="js/jquery.js"></script>
    <script type="text/javascript">
    $(document).ready(function() {

        $("#button").click(function() {

            var sendu = $("#username").val();
            var sendp = $("#password").val();

            $.ajax({
                type: "POST",
                url: "ajax.php",
                data: "username="+sendu+"&password="+sendp,
                dataType: "json",
                success: function(msg,string,jqXHR){
                    $("#result").html(msg.name);
                }

            });         

        });

    });


    </script>

</head>
<body>

    Name: <input type="text" id="username" name=""username /> <br />
    Password: <input type="password" id="password" name="password" /><br />

    <input type="button" value="send" id="button" />
    <div id="result"></div></p>

</body>
</html>

and this is the php file:

<?php 

$name = $_REQUEST['username'];
$password = $_REQUEST['password'];

$a = array('name'=>$name, 'password'=>$password);

$c = json_encode($list);

echo $c;


 ?>

so when i removed the dataType: "json" and when i removed the associative array from the php file, all goes well, it worked!

here is the after removing json encode:

html:

<script type="text/javascript">
$(document).ready(function() {

    $("#button").click(function() {

        var sendu = $("#username").val();
        var sendp = $("#password").val();

        $.ajax({
            type: "POST",
            url: "ajax.php",
            data: "username="+sendu+"&password="+sendp,

            success: function(msg,string,jqXHR){
                $("#result").html(msg);
            }

        });         

    });

});


</script>

and the php file:

<?php 

$name = $_REQUEST['username'];
$password = $_REQUEST['password'];


$c = "your name is ".$name."<br />and your password is ".$password;

echo $c;


 ?>

and one more thing that, i am not an english man so if you find any common grammer mistakes, please ignore!

  • 写回答

1条回答 默认 最新

  • douzhimei8259 2014-09-23 13:01
    关注

    One thing I noticed was this line in your AJAX call should be:

    data: "username="+sendu+"&password="+sendp,
    

    Should be:

    data: {username: sendu, password: sendp}
    

    That way you can access the variables when using dataType: JSON

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题