是否真的UIWebViewDelegate不监视使用XMLHttpRequest发出的请求?如果是这样,有没有办法监控这类请求?
例如,UIWebViewDelegate没有捕获到它-(BOOL) webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSMutableURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;
var xhr = new XMLHttpRequest();
xhr.open("GET", "http://www.google.com", true);
xhr.onreadystatechange=function()
{
if (xhr.readyState==4)
{
alert(xhr.responseText);
}
}
xhr.send();
Run Code Online (Sandbox Code Playgroud) 找出特定$.ajax()请求需要多长时间的好方法是什么?
我想获取此信息,然后在某个页面上显示它.
回答??::::
我是javascript的新手,如果你不想内联"成功"函数(因为它将是一个更大的功能),这是我能想到的最好的东西.这甚至是一个很好的方法吗?我觉得我太复杂了......:
makeRequest = function(){
// Set start time
var start_time = new Date().getTime();
$.ajax({
async : true,
success : getRquestSuccessFunction(start_time),
});
}
getRquestSuccessFunction = function(start_time){
return function(data, textStatus, request){
var request_time = new Date().getTime() - start_time;
}
}
Run Code Online (Sandbox Code Playgroud) var fd = new FormData();
fd.append("fileToUpload", document.getElementById('fileToUpload').files[0]);
var xhr = new XMLHttpRequest();
xhr.open("POST", "uph.php");
xhr.send(fd);
Run Code Online (Sandbox Code Playgroud)
uph.php:
var_dump($_FILES['fileToUpload']);
Run Code Online (Sandbox Code Playgroud)
这是有效的,但显然是files[0]唯一的.如何使这个适用于所选文件?
我尝试删除[0],但它没有用.
我试图检测XMLHttpRequest()由于交叉原始错误而不是错误请求而失败.例如:
ajaxObj=new XMLHttpRequest()
ajaxObj.open("GET", url, true);
ajaxObj.send(null);
Run Code Online (Sandbox Code Playgroud)
考虑4个网址案例:
情况1: url是正确设置access-control-allow-origin的有效地址
http://192.168.8.35我Access-Control-Allow-Origin: *在标头中设置了一个服务器情况2: url是现有服务器上的无效地址
http://xyz.google.com服务器响应的位置,但它不是有效请求情况3: url是一个不存在的服务器IP地址
http://192.168.8.6在我的本地网络上没有任何响应案例4: URL是一个有效的地址在访问控制允许来源是不设置
http://192.168.8.247我在标头中没有 Access-Control-Allow-Origin: *设置的服务器问题是:如何区分案例4(访问控制允许来源错误)和案例2和3?
在案例4中,Chrome调试控制台显示错误:
XMLHttpRequest cannot load http://192.168.8.247/. Origin http://localhost is not allowed by Access-Control-Allow-Origin.
如何在Javascript中识别该错误?
我试图找到一些迹象,ajaxObj但与案例2和3相比似乎没有什么不同.
这是我使用的简单测试:
<!DOCTYPE html PUBLIC "-//W3C//DTD …Run Code Online (Sandbox Code Playgroud) 我正在研究一些Javascript在我的电脑上本地运行.我正在使用jQuery CSV插件(http://plugins.jquery.com/project/csv)将csv文件加载到javascript数组中.脚本很简单:
$(function(){
$.get("file.csv", function(data){
stuff = $.csv()(data);
})
})
Run Code Online (Sandbox Code Playgroud)
在Firefox中它工作正常,但在Chrome中它说"Access-Control-Allow-Origin不允许使用Origin null".那是什么意思?我找到了与此错误相关的跨服务器内容的各种线程,但我只是使用本地文件.
我有这个代码:
net.requestXHR = function() {
this.xhr = null;
if(window.XMLHttpRequest === undefined) {
window.XMLHttpRequest = function() {
try {
// Use the latest version of the activex object if available
this.xhr = new ActiveXObject("Msxml2.XMLHTTP.6.0");
}
catch(e1) {
try {
// Otherwise fall back on an older version
this.xhr = new ActiveXObject("Mxsml2.XMLHTTP.3.0");
}
catch(e2) {
//Otherwise, throw an error
this.xhr = new Error("Ajax not supported in your browser");
}
}
};
}
else
this.xhr = new XMLHttpRequest();
}
net.requestXHR.prototype.post = function(url, data) …Run Code Online (Sandbox Code Playgroud) 我XMLHttpRequest在JavaScript中使用.但是,它给了我一个错误,我不知道我的问题是什么.
我必须解析XML文件并将其内容分配给网页 - 这是我的代码:
<script = "text/javascript">
window.onload = onPageLoad();
var questionNum = 0;
function onPageLoad(questionNum) {
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET","quiz.xml");
try {
xmlhttp.send(null); // Here a xmlhttprequestexception number 101 is thrown
} catch(err) {
document.getElementById("body").innerHTML += "\nXMLHttprequest error: " + err.description; // This prints "XMLHttprequest error: undefined" in the body.
}
xmlDoc = xmlhttp.responseXML;
parser = new DOMParser(); // This code is untested as it does not run this far.
}
</script>
Run Code Online (Sandbox Code Playgroud)
我的XML文件位于同一目录中.
<question>
<query>what …Run Code Online (Sandbox Code Playgroud) 我已经将更复杂的项目的编码风格改为最近"按需"加载页面(及其嵌入式脚本).但是,在加载它们时,很难调试这些脚本,如:
jQuery.get('/myModularPage', function(html){ /* insert the loaded page into the DOM */ });
Run Code Online (Sandbox Code Playgroud)
要么
$('#some-container').load('/myOtherPage');
Run Code Online (Sandbox Code Playgroud)
这些脚本运行完美,但如果我正在调试,如何在这些动态加载的页面和脚本中设置断点?
我只需要支持主流的现代浏览器(IE10 +,FF,Chrome,Safari)
我可以进行此替换,因为我想简化我的代码库:
从:
xhr.onreadystatechange = function () {
if (this.readyState === 4) {
if (this.status === 200) {
o.callback(xhr.responseText);
} else {
return false;
}
} else {
return false;
}
};
Run Code Online (Sandbox Code Playgroud)
至:
xhr.onload = function (test) {
o.callback(xhr.responseText);
};
Run Code Online (Sandbox Code Playgroud)
我不认为MDN文档在这方面是明确的.
澄清:
我选择不使用框架.
如果对我的php页面的请求是ajax请求,我想检查服务器端.
我看到了两种方法:
第一种方式:GET在请求中发送一个参数,告诉页面这是一个AJAX请求(= mypage.php?ajax)
mypage.php:
if(isset($_GET['ajax'])) {
//this is an ajax request, process data here.
}
Run Code Online (Sandbox Code Playgroud)
第二种方式:设置标题为xmlHttpRequest:
客户端js:
xmlHttpRequestObject.open(“GET”,url,true);
xmlHttpRequestObject.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
Run Code Online (Sandbox Code Playgroud)
mypage.php:
if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest' ) {
//request is ajax
}
Run Code Online (Sandbox Code Playgroud)
事实是,这两种方式很容易被黑客攻击,因此检查我是否收到这样的AJAX请求是不安全的.
如何检查我是否收到了AJAX请求?
xmlhttprequest ×10
javascript ×7
ajax ×6
jquery ×2
arrays ×1
cors ×1
cross-domain ×1
firebug ×1
php ×1
uiwebview ×1
upload ×1
xcode ×1
xml ×1