xmlhttprequest

XMLHttpRequest.status always returning 0

China☆狼群 提交于 2019-12-12 15:28:07
问题 html <a href="#" onclick="MyObj.startup()">click me</a> js code var MyObj = { startup : function() { var ajax = null; ajax = new XMLHttpRequest(); ajax.open('GET', 'http://www.nasa.gov', true); ajax.onreadystatechange = function(evt) { if(ajax.readyState == 4) { if (ajax.status == 200) { window.dump(":)\n"); } else { window.dump(":(\n"); } } } ajax.send(null); } } ajax.status always returning 0, no matter which site it is, no matter what is the actual return code. I say actual, because ajax

Why is the 'src' attribute allowed to link to scripts from external domains, and XmlHtppRequests not?

微笑、不失礼 提交于 2019-12-12 15:12:30
问题 I have read several answers on StackOverflow regarding same-origin policy, but I don't seem to graps the essential part. In all tags that use the src attribute, like <script> and <img> , you are allowed to use external resources (from another domain). Why is this allowed, but with a XMLHttpRequest (e.g. AJAX calls) it is not. I do not seem to graps why the latter is more dangerous. I mean, you could also have malicious code in an external source like: <script src="http://example.com/malicious

How dos XHR's “readyState==3” work?

ε祈祈猫儿з 提交于 2019-12-12 14:23:51
问题 Here's the code snippet xhr.onreadystatechange = function(){ if(xhr.readyState == 3){ console.log("readyState response length " + xhr.response.length); } } And the console is readyState response length: 3854 readyState response length: 33214 readyState response length: 35296 I don't know what's the timing of onreadystatechange firing, it does seems a bit related to the size of response,how does it work? 回答1: What is XHR readyState=3 ? Having the readyState with a value of 3 it means that the

How to call a javascript function from a JSON string?

。_饼干妹妹 提交于 2019-12-12 13:36:18
问题 If I do an AJAX post with jQuery that looks like $.post('MyApp/GetPostResult.json', function(data) { // what goes here? }); and the result looks like { "HasCallback": true, "Callback": "function(){ alert('I came from the server'); }" }; Then how do I call the Callback function? Can I just write if(data.HasCallback){data.Callback();} ? 回答1: This should work: function(data) { if(data.HasCallback) { eval(data.Callback); } } Edit: Didn't look quite carefully enough. If you're indeed getting the

Why does CORS not seem to work with POST?

微笑、不失礼 提交于 2019-12-12 13:17:37
问题 Mozilla's own specification says simple GET or POST should be natively CORS's without preflighting but so far every POST attempt I've made has resulted in an OPTIONS header going out. When I change it from POST to get the code immediately sends a proper GET request so the cross site part is working fine. Here's a slimmed down sample of what I'm doing in firefox: var destinationUrl = 'http://imaginarydevelopment.com/postURL'; var invocation = new XMLHttpRequest(); if (invocation) { invocation

How to debug XHR POST requests in Safari 4?

不问归期 提交于 2019-12-12 12:28:36
问题 I've got an XHR request that's succeeding in FF3.5 but apparently failing when done in Safari 4. I'm looking at the xhr requests in the debugger in Safari, and Firebug in Firefox. So on the backend service that's being hit, if the username and password are missing from the POST parameter then the service gives a 500 error. If the parameters are there(even if incorrect), it gives 200 or 40x depending on the error. Anyway, so with the same code its working for the non-Safari browsers. Safari

Ajax requests not running parallel in chrome on windows

懵懂的女人 提交于 2019-12-12 11:28:30
问题 I'm debugging a slow single page application (SugarCRM 7) that uses lots of asynchronous requests to fetch data from the server. I have set up the following script to test for possible delays in the connection/server etc. <?php if (isset($_GET['ajax'])) { if (isset($_GET['delay'])) { sleep((int) $_GET['delay']); } echo 'Success...'; exit(0); } ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title></title> </head> <body> <div id="main"></div> Number of requests: <input type=

How to display 'Loading' when making a 'synchronous' AJAX call in pure JavaScript?

十年热恋 提交于 2019-12-12 11:26:40
问题 I want to make sure the result is shown to user, so I make synchronous AJAX call. It's quite simple to display a 'Loading' indicator with asynchronous AJAX (the examples are everywhere), but when I use synchronous AJAX call with XMLHttpRequest, the loading indicator GIF image doesn't show up at all. Some said that it's impossible to show indicator when doing a synchronous call (block until having response from server). But I just want to ask to see whether there's a way to do it. 回答1: It's

How to get http response header in VBScript

对着背影说爱祢 提交于 2019-12-12 10:12:30
问题 Why this .vbs script retrieve null value? Option Explicit On Error Resume Next Dim h: Set h = CreateObject("MSXML2.XMLHTTP") h.Open "HEAD", "http://google.com/", False h.send Dim GetLocation: Set GetLocation = h.getResponseHeader("Location") MsgBox GetLocation 回答1: Almost all HTTP libraries follow redirects by default. Therefore you can not get Location header as long as you follow the redirects, so you need to stop following redirects. I recommend two different solutions. #1 Achieving the

Failed to override the getResponseHeader method on XMLHttpRequest

廉价感情. 提交于 2019-12-12 10:11:55
问题 I was trying to override the getResponseBody method on XMLHttpRequest object. The code looks so: xhr.onreadyStateChange = function(){ if (xhr.readyState !== 4) { return; } if (xhr.status === 200) { // callback to handle the result } else { var _orig = xhr.getResponseHeader; xhr.getResponseHeader = function(name){ return decodeHeader(_orig.apply(xhr,[name])); }; // callback to handle the failure } } It throws an error "Object doesn't support this property or method" when calling the _orig