Javascript 使用AJAX加载跨域端点
我试图使用AJAX加载跨域HTML页面,但除非数据类型为“jsonp”,否则我无法得到响应。但是,使用jsonp时,浏览器需要脚本mime类型,但收到“text/html” 我的请求代码是:Javascript 使用AJAX加载跨域端点,javascript,jquery,ajax,cross-domain,Javascript,Jquery,Ajax,Cross Domain,我试图使用AJAX加载跨域HTML页面,但除非数据类型为“jsonp”,否则我无法得到响应。但是,使用jsonp时,浏览器需要脚本mime类型,但收到“text/html” 我的请求代码是: $.ajax({ type: "GET", url: "http://saskatchewan.univ-ubs.fr:8080/SASStoredProcess/do?_username=DARTIES3-2012&_password=P@ssw0rd&_program=%
$.ajax({
type: "GET",
url: "http://saskatchewan.univ-ubs.fr:8080/SASStoredProcess/do?_username=DARTIES3-2012&_password=P@ssw0rd&_program=%2FUtilisateurs%2FDARTIES3-2012%2FMon+dossier%2Fanalyse_dc&annee=2012&ind=V&_action=execute",
dataType: "jsonp",
}).success( function( data ) {
$( 'div.ajax-field' ).html( data );
});
有没有办法避免对请求使用jsonp?我已经尝试过使用crossDomain参数,但它不起作用
如果没有,是否有任何方法可以在jsonp中接收html内容?当前控制台会说“意外,如果外部站点不支持JSONP或CORS,那么您唯一的选择就是使用代理
在您的服务器上构建一个请求该内容的脚本,然后使用jQuery ajax在您的服务器上点击该脚本。如果外部站点不支持JSONP或CORS,您唯一的选择就是使用代理 在您的服务器上构建一个请求该内容的脚本,然后使用jQuery ajax在您的服务器上点击该脚本。jQuery ajax注释
- 由于浏览器的安全性限制,大多数Ajax请求都受到限制;该请求无法成功地从不同的域、子域、端口或协议检索数据
- 脚本和JSONP请求不受同源策略限制
**警告** 使用第三方代理不是一种安全的做法,因为它们可以跟踪您的数据,因此它可以用于公共信息,但决不能用于私人数据
下面显示的代码示例使用了和,两者都是的速记方法
任何地方都可以 2021年更新 CORS Anywhere的演示服务器(CORS Anywhere.herokuapp.com)本应作为该项目的演示。但滥用已变得如此普遍,以至于演示所在的平台(Heroku)要求我关闭服务器,尽管已采取措施阻止滥用。由于滥用及其流行,宕机变得越来越频繁 为了解决这个问题,我将进行以下更改:
CORS Anywhere是一个node.js代理,它将CORS头添加到代理请求中。
要使用API,只需在URL前面加上API URL。(支持https:请参阅) 如果要在需要时自动启用跨域请求,请使用以下代码段:
$.ajaxPrefilter( function (options) {
if (options.crossDomain && jQuery.support.cors) {
var http = (window.location.protocol === 'http:' ? 'http:' : 'https:');
options.url = http + '//cors-anywhere.herokuapp.com/' + options.url;
//options.url = "http://cors.corsproxy.io/url=" + options.url;
}
});
$.get(
'http://en.wikipedia.org/wiki/Cross-origin_resource_sharing',
function (response) {
console.log("> ", response);
$("#viewer").html(response);
});
无论起源如何 是一种跨域jsonp访问。这是一种开源的替代方法 要从google.com获取数据,可以使用以下代码段:
// It is good specify the charset you expect.
// You can use the charset you want instead of utf-8.
// See details for scriptCharset and contentType options:
// http://api.jquery.com/jQuery.ajax/#jQuery-ajax-settings
$.ajaxSetup({
scriptCharset: "utf-8", //or "ISO-8859-1"
contentType: "application/json; charset=utf-8"
});
$.getJSON('http://whateverorigin.org/get?url=' +
encodeURIComponent('http://google.com') + '&callback=?',
function (data) {
console.log("> ", data);
//If the expected response is text/plain
$("#viewer").html(data.contents);
//If the expected response is JSON
//var response = $.parseJSON(data.contents);
});
CORS代理 CORS代理是一个简单的node.js代理,用于为任何网站启用CORS请求。 它允许站点上的javascript代码访问其他域上的资源,这些域通常由于同源策略而被阻止
www.corsproxy.com/
$.get(
'http://www.corsproxy.com/' +
'en.wikipedia.org/wiki/Cross-origin_resource_sharing',
function (response) {
console.log("> ", response);
$("#viewer").html(response);
});
CORS代理浏览器 最近我发现了这一个,它涉及各种面向安全的跨源远程共享实用程序。但它是一个以Flash为后端的黑匣子 您可以在此处看到它的作用:
在GitHub上获取源代码:jQuery Ajax Notes
- 由于浏览器的安全性限制,大多数Ajax请求都受到限制;该请求无法成功地从不同的域、子域、端口或协议检索数据
- 脚本和JSONP请求不受同源策略限制
**警告** 使用第三方代理不是一种安全的做法,因为它们可以跟踪您的数据,因此它可以用于公共信息,但决不能用于私人数据
下面显示的代码示例
$.ajax({
crossOrigin: true,
url: url,
success: function(data) {
console.log(data);
}
});
$('.div_class').load('http://en.wikipedia.org/wiki/Cross-origin_resource_sharing #toctitle');
// Create a listener.
HttpListener listener = new HttpListener();
// Add the prefixes.
//foreach (string s in prefixes)
//{
// listener.Prefixes.Add(s);
//}
listener.Prefixes.Add("http://*:1234/"); // accept connections from everywhere,
//because the printer is accessible only within the LAN (no portforwarding)
listener.Start();
Console.WriteLine("Listening...");
// Note: The GetContext method blocks while waiting for a request.
HttpListenerContext context;
string urlForRequest = "";
HttpWebRequest requestForPage = null;
HttpWebResponse responseForPage = null;
string responseForPageAsString = "";
while (true)
{
context = listener.GetContext();
HttpListenerRequest request = context.Request;
urlForRequest = request.RawUrl.Substring(1, request.RawUrl.Length - 1); // remove the slash, which separates the portNumber from the arg sent
Console.WriteLine(urlForRequest);
//Request for the html page:
requestForPage = (HttpWebRequest)WebRequest.Create(urlForRequest);
responseForPage = (HttpWebResponse)requestForPage.GetResponse();
responseForPageAsString = new StreamReader(responseForPage.GetResponseStream()).ReadToEnd();
// Obtain a response object.
HttpListenerResponse response = context.Response;
// Send back the response.
byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseForPageAsString);
// Get a response stream and write the response to it.
response.ContentLength64 = buffer.Length;
response.AddHeader("Access-Control-Allow-Origin", "*"); // the magic header in action ;-D
System.IO.Stream output = response.OutputStream;
output.Write(buffer, 0, buffer.Length);
// You must close the output stream.
output.Close();
//listener.Stop();
$.ajax({
type: 'POST',
url: 'http://LAN_IP:1234/http://google.com',
success: function (data) {
console.log("Success: " + data);
},
error: function (e) {
alert("Error: " + e);
console.log("Error: " + e);
}
});
var req = new XMLHttpRequest();
req.open('GET', 'http://localhost/get_url_content.php',false);
if(req.status == 200) {
alert(req.responseText);
}
header('Access-Control-Allow-Origin: *'); //allow everybody
header('Access-Control-Allow-Origin: http://codesheet.org'); //allow just one domain
$http_origin = $_SERVER['HTTP_ORIGIN']; //allow multiple domains
$allowed_domains = array(
'http://codesheet.org',
'http://stackoverflow.com'
);
if (in_array($http_origin, $allowed_domains))
{
header("Access-Control-Allow-Origin: $http_origin");
}