2016-07-21 12 views
-1
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns ="http://www.w3.org/1999/xhtml">   
<head>  
    <title>first_demo</title>  
</head> 

<script type="text/javascript" language = "javascript"> 
    //ajax call 
    $(document).ready(function(){ 
     $("#submit").click(function(){ 
      $.ajax({ 
       type: 'GET', 
       url: "hostname/filename.txt", 
       success:function(data){ 
        alert(data); 
       } 
      }); 
      return false; 
     }); 
    }); 
</script> 

<body> 
    <p>click to on </p> 
    <input type="button" id="submit" value="submit" /> 
</body> 
</html> 
+0

1)你腳本需要在'頭部'或''2之前)控制檯中是否有任何錯誤導致您的AJAX請求? –

+0

不,它不會拋出任何錯誤,我試圖把腳本放在頭部......但仍然無法正常工作。 –

回答

0

一個服務器上的文件試試這個可能:

HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html xmlns ="http://www.w3.org/1999/xhtml">  

<head> 

<title> first_demo </title> 

</head> 
<body> 
<p>click to on </p> 

<input type="button" id="submit" value="submit" /> 
<!--your ajax script comes here--> 
</body> 
</html> 

JAVASCRIPT

$("#submit").click(function(){ 
    $.ajax({ 
      type: "GET", 
      url: "hostname/filename.txt",//your url here 
      data: { 
       //Additional data 
      }, 
      dataType: 'html/text', 
      success: function(response) { 
       console.log("success: " + response); 
      }, 
      error: function(response) { 
       console.log("error: " + response); 
      } 
     }); 
    });  
+0

感謝您的答案,但我仍然無法打開服務器上的文件。 讓我們假設我的服務器名稱是:testing_server。 我使用上面的代碼創建了index.html並將其放置在testing_server的web文件夾中。 在ajax調用:url:「http://testing_server/on.txt」,所以我只在同一臺服務器上託管我的頁面。 你能幫忙嗎? –

+0

你使用什麼服務器? IIS,Apache?你確定文件不是隻讀嗎?您是否嘗試按F12並打開開發人員工具,並查看瀏覽器中的請求/響應是什麼? – ThatAwesomeCoder