weixin_33739523 2015-01-31 05:26 采纳率: 0%
浏览 32

jQuery无法加载Ajax

I am a bit new and trying to load text in a local text file using ajax through "XAMPP" requests, by jquery. I am doing everything correct, but still ajax data not loading from text-file to my div. Can anyone tell me why, as I am doing everything right. Here, goes snippet:

Jquery

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"/>
<script>
    $(document).ready(function(){ 
        $("#but1").click(function() {
            $("#div1").load("ajaxfile.txt");
        }); 
    });
</script>

CSS

*{
     margin:0px;
    padding:0px;
}
div{
    background:yellow;
    padding:30px;
    border:2px solid black;
    margin:20px;
}

Html

<button id=but1>One</button>
<div id=div1> 
   This is div:
</div> 

I can do it with javaScript, but, I want to do it with jQuery and by loading data from external txt file only. Also, if you may tell me, what changes will I make to load it with JSON format, and XML file format. Thank you dear!

@spirulence: Dear, for your request, I am posting the file which worked by 'javascript xmlhttp' rather than the 'jquery' method.

<script>
function loadXMLDoc()
{
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)
{
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","ajaxfile.txt",true);
xmlhttp.send();
}
</script>
  • 写回答

1条回答 默认 最新

  • weixin_33692284 2015-01-31 05:36
    关注

    If you are looking at downloading json then I recommend you to us:

    $.getJSON( "ajax/test.json", function( data ) {
    
      });
    

    For different content types, you can use $.get() and specify content types.

    Also, make sure that the path is relative and attached to the same domain otherwise you will face CORS issues.

    If you are getting 404 then you got to check that the file exists on server and your IIS Pool(whichever) user has rights to access the same.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算