根据chrome扩展API,如果设置了权限,则应允许使用XMLHttpRequest对象进行跨域调用:
扩展可以与其来源之外的远程服务器通信,只要它首先请求跨源权限即可.
我正在密切关注本教程,但下面的代码给出了一条错误消息:
XMLHttpRequest无法加载http://www.google.com/search?hl=en&q=ajax.原始chrome-extension:// bmehmboknpnjgjbmiaoidkkjfcgiimbo不允许使用Access-Control-Allow-Origin.
我不仅允许google.com的请求,而且要求任何网站但仍然无法通过.有人可以帮忙吗?
我的清单文件:
{
"name": "The popup",
"version": "0.1",
"popup": "popup.html",
"permissions": [
"http://*/*",
"https://*/*",
"https://www.google.com/*",
"http://www.google.com/*"
],
"browser_action": {
"default_icon": "clock-19.png",
"default_title": "This is title",
"default_popup": "popup.html"
}
}
Run Code Online (Sandbox Code Playgroud)
实际的电话:
function sendRequest() {
document.write("Sending request");
var req = new XMLHttpRequest();
req.open("GET", "http://www.google.com/search?hl=en&q=ajax", true);
req.onreadystatechange = function() {
if (req.readyState == 4) {
if (req.status == 200) {
alert(req.responseText);
document.write("OK");
}
}
};
req.send();
}
Run Code Online (Sandbox Code Playgroud) javascript ajax google-chrome xmlhttprequest google-chrome-extension
所以我正在考虑用户粘贴链接的功能,服务器端代码抓取提供的链接并响应该链接的内容(例如页面标题,描述,缩略图等).
用户可以同时更改链接,并且在这样做时,ajax-request应该在客户端中止.
我想知道IIS服务器到底发生了什么,特别是我的C#代码.
我有一个网站,我输入用户名/密码,然后单击登录按钮.登录按钮生成XMLHttpRequest对象,并将其触发.
在Chrome,Firefox,Opera,Safari,Android设备,iOS设备上运行正常.只要我在HTTP地址而不使用HTTPS,IE9就可以工作.
在HTTPS上,IE9的行为如下:
第一个登录请求永远不会返回任何内容.F12屏幕确实在网络选项卡中显示我的登录请求,所有看起来都正确.脚本选项卡永远不会抛出错误.没有任何反应.
这是疯狂的部分: - 如果我再次点击登录,它实际上是有效的. - 如果我点击浏览器上的刷新,然后登录,那也将有效!
我提出如下要求:
var x = new XMLHttpRequest();
x.open("POST", "/Relative/URL/Path", true);
x.setRequestHeader("Content-Type", "text/plain");
x.onreadystatechange = function () {
if ((x.readyState == 4) && (x.status == 200)) {
// handle callback
}
}
x.send(my request);
Run Code Online (Sandbox Code Playgroud)
当失败时,调试器将从x.send()行转到onreadystatechange代码.readyState将为1.这将是我可以调试的最后一个,因为没有其他事情发生.
任何想法都将非常感激.
[编辑]:我让一个请求去看看会发生什么.onreadystatechange事件再次使用readyState = 4和status = 12152触发.IE9的F12屏幕中的网络视图显示结果为Aborted,时间为1589.07s.Google搜索显示这意味着服务器上的连接已关闭.
[编辑2]:基于下面的评论,我将此代码重新编写为仅使用jQuery的ajax()方法.我认为这可能有可能消除我的错误代码.没有这样的运气.出现相同的行为.
$.ajax({
"url": sUrl,
"success": function (data, textStatus, x) {
workerCallback(data, id, "");
},
"error": function (x, testStatus, errorThrown) {
workerCallback("nc", id, errorThrown);
},
"contentType": "text/plain",
"data": JSON.stringify(req),
"dataType": "json",
"timeout": 1600000, …Run Code Online (Sandbox Code Playgroud) 在Chrome中,它会像它应该的那样执行HTTP PUT,但在FireFox 21中却没有.javascript控制台或后端没有错误.
这是HTML:
<div id="createTeamModal" class="small reveal-modal">
<form id="createTeamForm">
<div class="row"><p id="teamFlavorText" class="lead">Building a new team</p></div>
<div class="row">
<div class="small-4 large-4 columns"><label>Team Name:</label></div>
<div class="small-6 large-6 columns"><input name="teamName" id="teamName" type="text" size="20"/></div>
</div>
<div class="row"><p class="lead">Does this team work for a business?</p></div>
<div class="row">
<div class="small-4 large-4 columns"><label>Business Size:</label></div>
<div class="small-6 large-6 columns">
<select id="businessSizeSelect" name="businessSizeSelect">
<option value="1">Nope, I work alone</option><option value="2">2 to 49</option><option value="3">50 to 99</option><option value="4">100 to 999</option><option value="5">1,000+</option>
</select>
</div>
</div>
<div id="businessLocationDiv" class="row" style="display: none; margin-top: …Run Code Online (Sandbox Code Playgroud) 我正在尝试构建一个脚本,它将充当本机XMLHttpRequest对象的代理/包装器,使我能够拦截它,修改responseText并返回原始的onreadystatechange事件.
上下文是,如果应用程序尝试接收的数据已在本地存储中可用,则中止XMLHttpRequest并将本地存储的数据传递回应用程序成功/失败回调方法.假设我无法控制应用程序现有的AJAX回调方法.
我原本试过以下想法..
var send = XMLHttpRequest.prototype.send;
XMLHttpRequest.prototype.send = function(data){
//Do some stuff in here to modify the responseText
send.call(this, data);
};
Run Code Online (Sandbox Code Playgroud)
但正如我现在所建立的那样,responseText是只读的.
然后我尝试退后一步,编写自己的完整本机代理XMLHttpRequest,最终编写自己的本机方法版本.与此处讨论的相似......
http://www.ilinsky.com/articles/XMLHttpRequest/#implementation-wrapping
但它很快就变得混乱,并且仍然难以将修改后的数据返回到原始onReadyStateChange方法中.
有什么建议?这甚至可能吗?
我使用XMLHttpRequest发送到服务器端创建的可恢复上传URL,将基于浏览器的可恢复上传到Google的云存储中.这在禁用Web安全性时完全正常,我在开发期间执行了此操作.
但现在在现实世界中,CORS一直在制造麻烦.我也尝试过与其他浏览器(没有成功),但坚持使用chrome进行进一步测试.
注意:fake.host条目/etc/HOSTS用于欺骗chrome以避免localhost限制.然而,我们的在线测试服务器的"真实"域也是如此.
使用普通的XMLHttpRequest调用启动请求:
var xhr = this.newXMLHttpRequest();
xhr.open('PUT', url, true);
xhr.setRequestHeader('Content-Type', this.currentInputFile.type);
xhr.setRequestHeader('Content-Range', 'bytes ' + startByte + '-' + (this.currentInputFile.size - 1) + '/' + this.currentInputFile.size);
xhr.onload = function(e) {
...
};
...
if (startByte > 0) {
xhr.send(this.currentInputFile.slice(startByte));
} else {
xhr.send(this.currentInputFile);
}
Run Code Online (Sandbox Code Playgroud)
然后浏览器成功启动预检请求:
Remote Address:173.194.71.95:443
Request URL:https://www.googleapis.com/upload/storage/v1/b/my-bucket-name/o?uploadType=resumable&name=aa%20spacetestSMALL_512kb.mp4&upload_id=XXXXXXXXX
Request Method:OPTIONS
Status Code:200 OK
Run Code Online (Sandbox Code Playgroud)
请求标题:
:host:www.googleapis.com
:method:OPTIONS
:path:/upload/storage/v1/b/my-bucket-name/o?uploadType=resumable&name=aa%20spacetestSMALL_512kb.mp4&upload_id=XXXXXXXXX
:scheme:https
:version:HTTP/1.1
accept:*/*
accept-encoding:gzip,deflate
accept-language:en-US,en;q=0.8,de;q=0.6
access-control-request-headers:content-range, content-type
access-control-request-method:PUT
origin:https://fake.host
referer:https://fake.host/upload.xhtml
user-agent:Mozilla/5.0 (Macintosh; Intel Mac OS X …Run Code Online (Sandbox Code Playgroud) $.ajax({
url: "http://10.13.22.150/req_path",
success: function(result){
console.log(result);
}
});
Run Code Online (Sandbox Code Playgroud)
我想将跨域XMLHttpRequest发送到专用网络中的IP地址.但是,开发人员工具控制台中显示以下错误:
SCRIPT7002:XMLHttpRequest:网络错误0x2efd,由于错误00002efd无法完成操作.
根据Wireshark的说法,数据包不是从客户端发送的.我猜这个请求被Microsoft Edge阻止了.此外,我发现只有当XMLHttpRequest和Edge客户端的URL在私有网络的同一个CIDR中时才会阻止请求.
Client IP Request URL Result
192.168.x.x send to 192.168.x.x ->>>>> X
10.13.x.x send to 10.13.x.x ->>>>> X
10.13.x.x send to 192.168.x.x ->>>>> O
Run Code Online (Sandbox Code Playgroud)
其他浏览器如IE11/Chrome/Firefox也可以.此情况仅在Microsoft Edge中显示.有关此问题的解决方法或解决方案吗?
我有一个实现XMLHttpRequest接口的类.根据传递给的URL open(),我可以确定是使用默认的XMLHttpRequest还是我的自定义实现.我的想法是使用代理来执行此操作:
let xhr = new XHRProxy();
xhr.open('GET', 'http://blah'); // Decide here depending on URL
Run Code Online (Sandbox Code Playgroud)
我使用ES6代理进行了一些测试,看起来很有希望,但不幸的是,构建代理后无法修改代理目标:
var foo = {
name() {
return "foo";
}
};
var bar = {
name() {
return "bar";
}
}
var handler = {
get(target, property, receiver) {
if (property === "switchToBar") {
// FIXME: This doesn't work because a Proxy's target is not exposed AFAIK
receiver.target = bar;
return function() {};
} else {
return target[property];
}
}
}
var proxy = …Run Code Online (Sandbox Code Playgroud) 假设我有一个执行标准AJAX请求的函数:
function doXHR() {
var xhr = new XMLHttpRequest();
xhr.open('GET', 'https://jsonplaceholder.typicode.com/posts');
xhr.send();
xhr.onreadystatechange = () => {
console.log('ready state change');
};
}
doXHR();
console.log('done');
Run Code Online (Sandbox Code Playgroud)
这段代码会导致浏览器启动一个AJAX请求,但是在函数的什么时候请求实际启动了?根据这篇文章:https://blog.raananweber.com/2015/06/17/no-there-are-no-race-conditions-in-javascript/
[Calling
xhr.onreadystate()aftersend()]是可能的,因为HTTP请求仅在当前作用域结束其任务后执行.这使程序员能够在他希望的任何位置设置回调.由于JavaScript是单线程的,因此只有在这个单个线程可以自由运行新任务时才能发送请求.HTTP请求被添加到要执行的任务列表中,该任务由引擎管理.
但是当我在发送调用之后立即在devtools中添加断点时:
我收到了网络请求,虽然处于待处理状态:
在断点处,XHR readystate是1,即XMLHttpRequest.OPENED.据MDN的文档,XMLHttpRequest.OPENED是指xhr.open()被称为:https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/readyState
但是,如果我发表评论xhr.send(),只会.open()被称为:
然后我没有收到待处理的网络请求:
考虑到我可能需要函数范围结束以便实际发送请求,我将断点移到函数调用之后(并修改代码以查看XHR readyState):
但我仍然得到待处理的网络请求:
似乎调试器不仅冻结了代码执行,还冻结了任何网络请求.这是有道理的,因为您不希望在断点时完成网络请求,但这也使得无法确定网络请求何时实际启动.
我的问题是,请求实际发送到什么程度?调用xhr.send()只是设置请求,但要求函数范围在请求启动之前结束吗?xhr.send()在函数范围结束之前是否立即启动请求?或者还有其他事情发生在这里?
我正在通过XMLHttpRequest输入HTML表单的数据发送POST请求.不受JavaScript干扰的表单将提交其编码为的数据application/x-www-form-urlencoded.
使用XMLHttpRequest,我想通过FormDataAPI 发送数据,这不起作用,因为它将数据视为编码为multipart/form-data.因此,我需要将数据作为查询字符串,正确转义,写入到send方法中XMLHttpRequest.
addEntryForm.addEventListener('submit', function(event) {
// Gather form data
var formData = new FormData(this);
// Array to store the stringified and encoded key-value-pairs.
var parameters = []
for (var pair of formData.entries()) {
parameters.push(
encodeURIComponent(pair[0]) + '=' +
encodeURIComponent(pair[1])
);
}
var httpRequest = new XMLHttpRequest();
httpRequest.open(form.method, form.action);
httpRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
httpRequest.onreadystatechange = function() {
if (httpRequest.readyState === XMLHttpRequest.DONE) {
if (httpRequest.status === 200) {
console.log('Successfully submitted the request');
} …Run Code Online (Sandbox Code Playgroud) xmlhttprequest ×10
javascript ×7
ajax ×6
cors ×2
c# ×1
debugging ×1
ecmascript-6 ×1
firefox ×1
form-data ×1
http-put ×1
iis ×1
jquery ×1
ssl ×1