douhuanchi6586 2015-08-11 16:35
浏览 56

Ajax函数返回总是错误

I need to pass some value to a PHP file and from this return some values to the original script. I wrote this code.

$(document).ready(function() {
    var value = $('#myId').val();
    $('.myClass').click(function() {
        var request = $.ajax({
            type:   'POST',
            url:    'getinfo.php',
            data:   {fileIs: value},
            success: function(data) {
                alert(data);
            },
            error: function() {
                alert("Error"); 
            }
        });

        });   
    });

getinfo.php is like:

mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("my_db") or die(mysql_error());

if(isset($_REQUEST['fileIs'])) {
    $file = mysql_real_escape_string(htmlentities($_POST['fileIs']));
    $value = mysql_query("SELECT * FROM files WHERE filename = '".$file."' ");
    while($row = mysql_fetch_array($value)) {
        echo $row['fileTitle']; 
    }       
}

When i execute the script php AJAX function always return the alert with wrote "Error" that disappear automatically. How can i fix this problem? My goal is to send "value" variable content to the PHP file and get response from it to have the content in database corresponding to "value" content.

  • 写回答

2条回答 默认 最新

  • double820122 2015-08-11 17:37
    关注

    The ajax function can't tell what type of data is being returned because you aren't setting the header of the http response in the php code. Adding the following before the while loop should help:

    header("Content-type: text/plain; charset=utf-8");
    

    Documentation: http://php.net/manual/en/function.header.php

    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看