jQuery的$。阿贾克斯的dataType JSON的请求将无法检索PHP脚本的数据 [英] jQuery $.ajax request of dataType json will not retrieve data from PHP script

查看:228
本文介绍了jQuery的$。阿贾克斯的dataType JSON的请求将无法检索PHP脚本的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找全国各地的解决方案,但我无法找到任何工作。 我试图让一群从数据库中的数据,然后通过在表单中​​的AJAX自动完成输入字段。要做到这一点,我已经决定使用JSON,因为为什么不呢,对不对?另外我一直在想,只是发回一个分隔字符串,然后tokenise它,这在后视线会一直更容易,饶过我头痛......既然我已经决定,虽然使用JSON,我想我应该坚持下去,并找出了问题! 什么情况是,get_member_function()被执行时,出现错误,在警告对话,并写着[对象的对象]。我已经试过这也使用GET请求,并通过设置的contentType为application / json的;字符集= UTF-8。唉,没有骰子。任何人都可以请建议什么,我做错了什么?请注意,彼得。

我的JavaScript / jQuery的功能如下:

 函数get_member_info()
   {

   VAR URL =内容/ php_scripts / admin_scripts.php;
   VAR ID = $(选择[名称=成员]选项:选择).VAL();

   $阿贾克斯(
   {

      键入:POST,
      数据类型:JSON,
      网址:网址,
      数据:{get_member:ID},
      成功:函数(响应)
      {

          $(输入[名称=类型]:EQ(+ response.type +)).attr(选中,检查);
          $(输入[名称=名称]).VAL(response.name);
          $(输入[名称= FNAME]).VAL(response.fname);
          $(输入[名称= L-NAME]).VAL(response.lname);
          $(输入[名称=电子邮件]).VAL(response.email);
          $(输入[名称=电话]).VAL(response.phone);
          $(输入[名称=网]).VAL(response.website);
          $(#admin_member_img).attr(SRC,图像/ member_images /+ response.image);

      },
      错误:功能(错误)
      {

         警报(错误);

      }

   });

}
 

和相关的code。在内容/ php_scripts / admin_scripts.php如下:

 如果(使用isset($ _ POST [get_member]))
   {

      $ member_id = $ _ POST [get_member];
      $查询=选择*从成员其中id ='$ member_id';

      $结果= mysql_query($查询);

      $行= mysql_fetch_array($结果);

      $型= $行[型];
      $名称= $行[名称];
      $ FNAME = $行[FNAME];
      $ L-NAME = $行[L-NAME];
      $电子邮件= $行[电子邮件];
      $手机= $行[电话];
      $网站= $行[网址];
      $图像= $行[形象];

      $ json_arr =阵列(输入=> $类型,名=> $名,其中fname=> $ FNAME,LNAME=> $ LNAME,电子邮件=> $电子邮件, 手机=> $的电话,网站=> $网站,图像=> $图像);

      回声json_en code($ json_arr);

   }
 

解决方案

我想我知道这样的产品......

尝试使用PHP的 header()函数发送您的JSON作为JSON 功能:

  / **
 *发送JSON
 * /
标题(内容类型:应用程序/ JSON,真正的);
 

虽然你是通过有效的JSON,jQuery的$就不会这么认为,因为它缺少了头。

jQuery的使用要罚款没有头,但它改变了几个版本了。

同时

请确保您的脚本返回有效JSON。使用萤火虫谷歌Chrome浏览器的开发者工具的检查控制台请求的响应。

更新

您还需要更新您的code消毒的$ _ POST,以避免SQL注入攻击。除了提供一些错误捕获。

 如果(使用isset($ _ POST ['get_member'])){

    $ member_id = mysql_real_escape_string($ _ POST [get_member]);

    $查询=SELECT * FROM`members` WHERE`ID` ='。 $ member_id。 ';;

    如果($结果= mysql_query($查询)){

       $行= mysql_fetch_array($结果);

       $型= $行['类型'];
       $名称= $行['名称'];
       $ FNAME = $行['FNAME'];
       $ L-NAME = $行['LNAME'];
       $电子邮件= $行[电子邮件];
       $手机= $行['电话'];
       $网站= $行['网站'];
       $图像= $行[形象];

       / * JSON行* /
       $ JSON =阵列(输入=> $类型,名=> $名,其中fname=> $ FNAME,LNAME=> $ LNAME,电子邮件=> $电子邮件, 手机=> $的电话,网站=> $网站,图像=> $图像);

    } 其他 {

        / *您查询失败,用mysql_error报告为什么* /
        $ JSON =阵列('错误'=>MySQL查询错误);

    }

     / *发送JSON * /
     标题(内容类型:应用程序/ JSON,真正的);

    / *返回JSON * /
    回声json_en code($ JSON);

    / *停止执行* /
    出口;

}
 

I've been looking all over for the solution but I cannot find anything that works. I am trying to get a bunch of data from the database and then via AJAX autocomplete input fields in a form. To do this I've decided to use json, because why not, right? Alternatively I've been thinking to just send back a delimited string and then tokenise it, which in hind-sight would've been much easier and spared me the headache... Since I've decided to use json though, I guess I should stick with it and find out what went wrong! What happens is that when the get_member_function() is executed, an error pops up in an alert dialogue and reads "[object Object]". I've tried this also using the GET request, and by setting the contentType to "application/json; charset=utf-8″. Alas, no dice. Can anyone please suggest what I am doing wrong? Take care, Piotr.

My javascript/jQuery function is as follows:

function get_member_info()
   {

   var url = "contents/php_scripts/admin_scripts.php"; 
   var id = $( "select[ name = member ] option:selected" ).val();

   $.ajax(
   {

      type: "POST",
      dataType: "json",
      url: url,
      data: { get_member: id },
      success: function( response ) 
      { 

          $( "input[ name = type ]:eq( " + response.type + " )" ).attr( "checked", "checked" );
          $( "input[ name = name ]" ).val( response.name );
          $( "input[ name = fname ]" ).val( response.fname );
          $( "input[ name = lname ]" ).val( response.lname );
          $( "input[ name = email ]" ).val( response.email );
          $( "input[ name = phone ]" ).val( response.phone );
          $( "input[ name = website ]" ).val( response.website );
          $( "#admin_member_img" ).attr( "src", "images/member_images/" + response.image );

      },
      error: function( error )
      {

         alert( error );

      }

   } );

}

and the relevant code in "contents/php_scripts/admin_scripts.php" is as follows:

   if( isset( $_POST[ "get_member" ] ) )
   {

      $member_id = $_POST[ "get_member" ];
      $query = "select * from members where id = '$member_id'";

      $result = mysql_query( $query );

      $row = mysql_fetch_array( $result );

      $type = $row[ "type" ];
      $name = $row[ "name" ];
      $fname = $row[ "fname" ];
      $lname = $row[ "lname" ];
      $email = $row[ "email" ];
      $phone = $row[ "phone" ];
      $website = $row[ "website" ];
      $image = $row[ "image" ];

      $json_arr = array( "type" => $type, "name" => $name, "fname" => $fname, "lname" => $lname, "email" => $email, "phone" => $phone, "website" => $website, "image" => $image );

      echo json_encode( $json_arr );

   }

解决方案

I think I know this one...

Try sending your JSON as JSON by using PHP's header() function:

/**
 * Send as JSON
 */
header("Content-Type: application/json", true);

Though you are passing valid JSON, jQuery's $.ajax doesn't think so because it's missing the header.

jQuery used to be fine without the header, but it was changed a few versions back.

ALSO

Be sure that your script is returning valid JSON. Use Firebug or Google Chrome's Developer Tools to check the request's response in the console.

UPDATE

You will also want to update your code to sanitize the $_POST to avoid sql injection attacks. As well as provide some error catching.

if (isset($_POST['get_member'])) {

    $member_id = mysql_real_escape_string ($_POST["get_member"]);

    $query = "SELECT * FROM `members` WHERE `id` = '" . $member_id . "';";

    if ($result = mysql_query( $query )) {

       $row = mysql_fetch_array($result);

       $type = $row['type'];
       $name = $row['name'];
       $fname = $row['fname'];
       $lname = $row['lname'];
       $email = $row['email'];
       $phone = $row['phone'];
       $website = $row['website'];
       $image = $row['image'];

       /* JSON Row */
       $json = array( "type" => $type, "name" => $name, "fname" => $fname, "lname" => $lname, "email" => $email, "phone" => $phone, "website" => $website, "image" => $image );

    } else {

        /* Your Query Failed, use mysql_error to report why */
        $json = array('error' => 'MySQL Query Error');

    }

     /* Send as JSON */
     header("Content-Type: application/json", true);

    /* Return JSON */
    echo json_encode($json);

    /* Stop Execution */
    exit;

}

这篇关于jQuery的$。阿贾克斯的dataType JSON的请求将无法检索PHP脚本的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆