Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/389.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 加载文本文件时如何在jQueryAjax中使用jsonop_Javascript_Jquery_Ajax - Fatal编程技术网

Javascript 加载文本文件时如何在jQueryAjax中使用jsonop

Javascript 加载文本文件时如何在jQueryAjax中使用jsonop,javascript,jquery,ajax,Javascript,Jquery,Ajax,这个问题与我的上一个问题有关,这个问题仍然没有得到回答: 我正在尝试使用jquery ajax方法在html页面中加载文本文件,html和文本托管在localserver上。在这种情况下,我应该如何使用回调调用“jsonp”方法 我的代码 <!DOCTYPE html> <html> <head> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"

这个问题与我的上一个问题有关,这个问题仍然没有得到回答:

我正在尝试使用jquery ajax方法在html页面中加载文本文件,html和文本托管在localserver上。在这种情况下,我应该如何使用回调调用“jsonp”方法

我的代码

<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>

<script language="javascript" type="text/javascript">
    $(document).ready(function () {
        $("button").click(function () {
         $.ajax({
     url:"http://localhost/demo_test.txt",
     dataType: 'jsonp', 
     success:function(){

         alert("Success");
     },
     error:function(){
         alert("Error");
     },
});
           return false;
        });
    });
</script>
</head>
<body>

<div id="div1"><h2>Let jQuery AJAX Change This Text</h2></div>
<button>Get External Content</button>

</body>
</html>

$(文档).ready(函数(){
$(“按钮”)。单击(函数(){
$.ajax({
url:“http://localhost/demo_test.txt",
数据类型:“jsonp”,
成功:函数(){
警惕(“成功”);
},
错误:函数(){
警报(“错误”);
},
});
返回false;
});
});
让jQuery AJAX更改此文本
获取外部内容

请建议。每次我都会遇到跨域错误

JQuery ajax函数可能有一个跨域请求,它是jsonp,但您不能这样使用它

响应jsonp请求的服务器端实际上是一个javascript函数,例如:

js文件:

$("button").click(function () {
    $.ajax({
       url: 'http://xxx.xxx.com/yoururl',
       dataType: 'jsonp',
       success: function(data) {
           console.log(data)
       }
});
服务器端(例如php)



在代码中,txt文件不是有效的jsonp响应

好的。我终于成功了: 以下是工作代码:

<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>

<script language="javascript" type="text/javascript">
 <!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
  $("button").click(function(){
    $.ajax({url:"http://localhost/demo_test.txt",success:function(result){
      $("#div1").html(result);
    }});
  });
});
</script>
</head>
<body>

<div id="div1"><h2>Let jQuery AJAX Change This Text</h2></div>
<button>Get External Content</button>

</body>
</html>

</script>
</head>

$(文档).ready(函数(){
$(“按钮”)。单击(函数(){
$.ajax({url:http://localhost/demo_test.txt,成功:函数(结果){
$(“#div1”).html(结果);
}});
});
});
让jQuery AJAX更改此文本
获取外部内容

您说过您的html和txt托管在同一个本地服务器上,那么就没有必要使用jsonp,因为它们位于同一个域中。嗯,也许你只是说它们在同一台没有http服务器的计算机上?那么如何使用jquery ajax加载文本文件呢?我只想实现教程中的一个示例代码,但它不起作用,因为longOk,我开始获取文本文件的数据,但html页面的按钮(获取外部内容)仍然保留在文本文件中。如何删除它
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>

<script language="javascript" type="text/javascript">
 <!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
  $("button").click(function(){
    $.ajax({url:"http://localhost/demo_test.txt",success:function(result){
      $("#div1").html(result);
    }});
  });
});
</script>
</head>
<body>

<div id="div1"><h2>Let jQuery AJAX Change This Text</h2></div>
<button>Get External Content</button>

</body>
</html>

</script>
</head>