使用Ajax写入服务器文本文件 [英] Write server text files with Ajax

查看:813
本文介绍了使用Ajax写入服务器文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力用Ajax编写服务器文本文件,如果有人有时间看看,我会非常感激.简单来说,以下代码为什么不将'testdata'写入test1.txt?

I've been struggling to write server text files with Ajax and would really appreaciate if someone had a moment to take a look. In simple, why doesnt the following code write 'testdata' to test1.txt?

<!DOCTYPE html>
<html>
<head>
<script>

var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
 if (xmlhttp.readyState==4 && xmlhttp.status==200)
  {
alert('done')
  }
}

xmlhttp.open("POST","test1.txt",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("testdata");


</script>
</head>
<body>

</body>
</html>

我已经能够使用GET成功读取文本文件.如果我将3个关键行替换为

I've successfully been able to read text files with GET. If I replace the 3 key lines with

xmlhttp.open("GET","test1.txt",true);
xmlhttp.send();

有效.

上面的代码有什么问题,或者这是文件权限问题?我正在使用GoDaddy,并已获得写作许可,以便可以使用php修改上述文本文件.

What is wrong with the code above or is this a file permission issue? I am using GoDaddy and given writing permission so that I can modify the above text file with php for example.

非常感谢您的帮助.

提前谢谢!

乔尔

推荐答案

现在可以正常使用了-感谢Alex!这些是工作文件:

Got it working now - thanks Alex! These are the working files:

<!DOCTYPE html>
<html>
<head>
<script>

var xmlhttp;
if (window.XMLHttpRequest)
 {// code for IE7+, Firefox, Chrome, Opera, Safari
 xmlhttp=new XMLHttpRequest();
 }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
  {
alert('done')
 }
}

xmlhttp.open("POST","phpwrite2.php",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("name=Joel");


</script>
</head>
<body>

</body>
</html>

和PHP:

<?php
$myFile = "ttt.txt";

$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = $_POST["name"];
fwrite($fh, $stringData);
fclose($fh);
?>  

这篇关于使用Ajax写入服务器文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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