JSON没有返回对象

时间:2014-06-12 05:36:53

标签: javascript php jquery ajax json

JSON数组不返回对象而是返回值

HI朋友我有一个场景,其中用户输入他的memberid我用memberid做一个jquery ajax并获取与他相关的详细信息并将其放在文本框中,

当代码部署到其他服务器但我没有得到结果时,它在我的服务器上工作正常

代码如下所示

  1. createaccount.php

                                                                                                                                               

                <title>Century Club</title>
            <link href='http://fonts.googleapis.com/css?family=Oswald' rel='stylesheet' type='text/css'>
            <link href='http://fonts.googleapis.com/css?family=Open+Sans' rel='stylesheet' type='text/css'>
    
    
                <link href="css/century-club.css" rel="stylesheet">
    
    
    
            <script src="js/jquery-1.4.2.min.js" type="text/javascript" charset="utf-8"></script>
    
    
                <script type="text/javascript">
    
    
                    function check()
                    {       
    
                        var mid = $("#memberid").val();
    
    
                        var dataString = "mid=" + mid ;
    
                            $.ajax({  
                                type: "POST",  
                                url: "getdetails2.php",  
                                datatype: 'json',
                                data: dataString,
                                beforeSend: function() 
                                {
                                    $('#process').html($('#status').html());
                                },  
                                success: function(data)
                                {
                                        $('#process').html('');
                                         alert(data);
                                        $.each(data, function (i,member) {
                                            $("#name").val(member.name);
    
                                        });
    
                                }   
                            });
    
                    }//End of SecureLogin 
    
    
                </script>
    
              </head>
    
              <body>
                <div class="container">
    
    
                        <div class="menu">
                        <span class="nav_top"></span>
                        <div class="clr"></div>
                            <nav id="menu-wrap">    
    
            </nav>
                        </div>
                </div>  
    
                 <div class="container">
                    <div class="register">
                     <div class="row">
                    <div class="col-xs-12 col-md-9">
                         <div class="matter"><div style="clear:both; height:15px;"></div>
                <center><h1>Create New Account</h1></center>
                <div style="clear:both; height:8px;"></div>
    
                     <div class="join-club">
                        <h4 class="join-heading">Century Club - Create Account <br><span class="legend"><font color="#FF0000"><font class="red"> *</font></font> indicates a mandatory field</span></h4>
    
    
                <div align="center" id="status" style="display:none">
                        Just Wait a Moment..</div>
                        <div id="process" class="process"></div>          
            <form id="register" name="register"   method="POST">            
                 <table class="join-members" width="100%" style="margin:10px 0 0 0; ">
                    <tbody>
    
                    <tr>
                        <td width="47%"><span>User Name (Membership Account No.)<font class="red"> *</font></span>
                            <p>(Example : abcd1234)</p>
                        </td>
                        <td colspan="2">
                            <input   type="text" name="memberid" id="memberid"  Maxlength="15"
                            value="<?php if(!empty($memberid)) echo $memberid;?>" autocomplete="off"  style="width: 95%;
                        padding: 6px;" class="email2"  onblur="check();"   /></td>
                    </tr>
    
                    <tr>
                    <td><span>Name <font class="red"> *</font></span></td>
    
                        <td colspan="2"><input  type="text" name="name" id="name"  readonly
                        value="<?php if(!empty($name)) echo $name;?>"  class="email"/></td>
                    </tr>
    
                    <tr>
                        <td >
                        </td>
                        <td colspan="2">  <input type="submit"   name='submit_req' value="Submit" class="button"  style="margin:0"  /> </td>
                    </tr>
                    </tbody>
                 </table>
            </form>
    
                  </div>
                </div>
                    </div>
                    </div>
                        <div class="clr"></div>
                    </div>
    
                    </div>
                 </div>
    
               </body>
            </html>
    
  2. JSon响应文件getdetails2.php

     <?php
        include("connection.php");
        $mid=trim($_POST['mid']);
    
        $query="Select member_id,member_name,office_number,mobile_number,Residence_number from cm_details where member_id='$mid'";
        $data=mysqli_query($dbc,$query);
    
        //while loop starts here buddy
        while($row=mysqli_fetch_array($data))
            {
                $mid=$row[0];
                $mname=$row[1];
                $rows[] = array("name" => $mname);
            }//end of while loop here
    
        @header("Content-type: application/json");
        echo json_encode($rows);
    ?>
    

    根据分析,它将JSON响应视为我服务器中的一个对象,其中代码工作正常,但在另一个服务器中它并不将它作为对象处理

    我注意到的另一个错误是

     Uncaught TypeError: Cannot use 'in' operator to search for '182' in
     [{"name":"SRI.M.S.ASHOKKUMAR","mobile":"9845901242","rescode":"080","resnum":"25253602","rescode1":"","resnum1":"","offcode":"","offnum":"","offcode1":"","offnum1":"","mobile1":""}]
    

    多数民众赞成我可以做的人我觉得JSON没有被服务器识别出类似的东西帮助我们尽快期待你的答案

1 个答案:

答案 0 :(得分:1)

您看到的是什么类型的错误/问题?也可以使用header而不是@header,以便您可以看到错误消息。

你在服务器上检查了你的php版本吗? json_encode / json_decode在php 5.2中引入。

如果这是问题,您可能需要使用类似jsonwrapper的内容。