如何在jQuery中显示.txt文件 [英] how to display a .txt file in jquery

查看:188
本文介绍了如何在jQuery中显示.txt文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个textarea和这个javascript代码它应该工作,但不是有什么问题吗?如果是这样,那是什么?



  $('#text')。load(http://hokuco.com/test/xe7/user.txt);   

< form action =formcode.phpmethod = POST>< textarea name ='field2'placeholder ='Code here'rows =40cols =40id =text>< / textarea>< br />< input type =submitname =submitvalue =Save>< / form>


$ b

这里是我的php文件,如果好奇的话:

<?phpif(isset($ _ POST ['field1 '])&& isset($ _ POST ['field2'])){$ data = $ _POST ['field1']。 ' - '。 $ _POST ['field2']。 \\\
; $ ret = file_put_contents('code.txt',$ data,FILE_APPEND | LOCK_EX); if($ ret === false){die('写这个文件时出错'); } else {echo$ ret bytes write to file;



我假定.txt文件不在同一台服务器/域中作为试图加载它的HTML文件?如果你是,那么你应该使用相对URL,即:

  $('#text')。load(xe7 /user.txt); 

请参阅:


I have a textarea and this javascript code It should work but dosen't is there something wrong? If so, what is it?

$('#text').load("http://hokuco.com/test/xe7/user.txt");

<form action="formcode.php" method="POST">
<textarea name='field2' placeholder='Code here' rows ="40" cols="40" id ="text"></textarea>
<br />
<input type="submit" name="submit" value="Save">
</form>

here is my php file if curious:

<?php
if(isset($_POST['field1']) && isset($_POST['field2'])) {
    $data = $_POST['field1'] . '-' . $_POST['field2'] . "\n";
    $ret = file_put_contents('code.txt', $data, FILE_APPEND | LOCK_EX);
    if($ret === false) {
        die('There was an error writing this file');
    }
    else {
        echo "$ret bytes written to file";
    }
}
else {
   die('no post data to process');
}
?>

解决方案

Due to browser security restrictions, most Ajax requests are subject to the same origin policy; the request can not successfully retrieve data from a different domain, subdomain, port, or protocol.

I'm assuming the .txt file is not on the same server/domain as the HTML file that is trying to load it? If you are, then you should use a relative URL, i.e.:

$('#text').load("xe7/user.txt");

Please refer to: Loading cross domain endpoint with jQuery AJAX

这篇关于如何在jQuery中显示.txt文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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