dqoys62082 2014-08-24 23:42
浏览 620

XMLHttpRequest没有返回值

i am unable to receive a response from the php file.

Javascript

var http = new XMLHttpRequest();
var url = "http://localhost/php_bollywood_romance.php";
var params = "film="+movie_name_entered;
http.open("POST", url, true);
//Send the proper header information along with the request
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.setRequestHeader("Content-length", params.length);
http.setRequestHeader("Connection", "close");

http.onreadystatechange = function() {//Call a function when the state changes.
    if(http.readyState == 4 && http.status == 200) {
        alert(http.responseText);
    }
}
http.send(params);

I have even tried to do this.....

 var http = new XMLHttpRequest();
 if ("withCredentials" in http)
 {
    var url = "http://localhost/php_bollywood_romance.php";
var movie_name_entered="hi";
var params = "film="+movie_name_entered;
http.open("POST", url, true);
 }
 else if (typeof XDomainRequest != "undefined"){
    http = new XDomainRequest();
    var url = "http://localhost/php_bollywood_romance.php";
    var movie_name_entered="hi";
    var params = "film="+movie_name_entered;
    http.open("POST", url);
} else {
    http = null;
}
if (http){
http.onload = function(){
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.setRequestHeader("Content-length", params.length);
http.setRequestHeader("Connection", "close");

http.onreadystatechange = function() {//Call a function when the state changes.
    if(http.readyState == 4 && http.status == 200) {
        document.write(http.responseText);
        callback(http.responseText);
        alert("hey");
    }
}
http.send(params);
};

PHP

  <?php
    {
     $name = $_POST["film"];
      $file ='data_bollywood_romance.txt';
       $current=file_get_contents($file); //gets existing content from the file
    $current ="$current 
 $name 
";
   file_put_contents($file,$current); //put newstuff
    echo 'hi';
   return 'yes';
     }
   ?>

However this also does not seem to work... if i do alert("http.response") after http.send(params)...it alerts an alert with nothing written on it. I thought this might be because there is same origin policy problem so i tried to do Cross-domain Ajax with Cross-Origin Resource Sharing but that didnt work either( that is the second code). Thanks for your help in advance.

  • 写回答

1条回答 默认 最新

  • doutuo3899 2014-08-24 23:53
    关注

    You cannot set this two headers:

    http.setRequestHeader("Content-length", params.length);
    http.setRequestHeader("Connection", "close");
    

    Previous thread about your problem

    I tried it without and worked fine.

    Personally i would go all the way using jQuery Ajax, but thats just "lazy me" :)

    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?