[英]jQuery Get Request on HTTP URL
我最近嘗試使用jQuery從URL獲取一些響應。 因此,我將jQuery API Get Request Tutorial的get請求示例復制到我的項目中並嘗試運行它,但是我的調試消息向我顯示,它無法繼續下去。 我使用簡單的請求嘗試了javascript Ajax庫,但它沒有用。
所以我問你,如果你能以某種方式幫助我。
這就是我所做的一切,但沒有回應。
var url = "http://www.google.com";
$.get(url, function(data){
alert("Data Loaded: " + data);
});
我可能忘了包含一個ajax或jQuery庫。 為了更好地理解,我有c和obj-c經驗,這就是我認為庫缺失的原因。
在每個樣本中只有一個簡短的網址,如“test.php”。 我的完整HTTP網址是錯誤的嗎?
謝謝你的高級答案。
Br Nic
我提供了一個示例場景來幫助您入門:
<!-- Include this jQuery library in your HTML somewhere: -->
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.0/jquery.min.js"></script
這可能最好包含在外部JS文件中:
//Listen when a button, with a class of "myButton", is clicked
//You can use any jQuery/JavaScript event that you'd like to trigger the call
$('.myButton').click(function() {
//Send the AJAX call to the server
$.ajax({
//The URL to process the request
'url' : 'page.php',
//The type of request, also known as the "method" in HTML forms
//Can be 'GET' or 'POST'
'type' : 'GET',
//Any post-data/get-data parameters
//This is optional
'data' : {
'paramater1' : 'value',
'parameter2' : 'another value'
},
//The response from the server
'success' : function(data) {
//You can use any jQuery/JavaScript here!!!
if (data == "success") {
alert('request sent!');
}
}
});
});
您正在針對ajax請求達到同源策略 。
簡而言之,默認情況下,JS / Ajax只允許在與提供HTML頁面的域相同的域上觸發請求。 如果您打算在其他域上觸發請求,則必須支持JSONP和/或設置Access-Control
標頭以使其正常工作。 如果這不是一個選項,那么你必須在服務器端創建一些代理並改為使用它(小心,因為你可以禁止使用機器人從其他站點過度使用)。
正如其他人所說,你無法訪問另一台服務器上的文件。 有一個hack tho。 如果您使用的是服務器端語言(我假設您是這樣),您可以執行以下操作:
http://myserver.com/google.php :
<?php
echo get_file_contents('http://www.google.com');
?>
http://myserver.com/myscript.js
$.get('google.php',function(data){ console.log(data) });
這應該工作!
您只需訪問域/服務器中的頁面即可
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.