I get the following error in Firefox 31 ESR:
Error: NS_ERROR_FAILURE:
Source file:
Here is the same thing from Internet Explorer 11:
SCRIPT5022: InvalidStateError
Here is my script that does the call to the AJAX function:
ajax('post','standard/','part='+p+'&price='+q,'sequel_sub_object_key');
Here is my AJAX function:
function ajax(method,url,param_id_container_pos,id_container,id_focus,sequel)
{
var methods = Array('get','head','post');
if (window.XMLHttpRequest && in_array(method,methods))
{
xhr = new XMLHttpRequest();
xhr.open(method,url,true);//Error triggers here for localhost.
if (method=='post')
{
xhr.setRequestHeader('Content-type','application/x-www-form-urlencoded');
xhr.send(param_id_container_pos);
}
xhr.send(null);
xhr.onreadystatechange = function()
{console.log('xhr.readyState = '+xhr.readyState);
if (xhr.readyState=='4')
{
alert('xhr.responseText = '+xhr.responseText);
}
}
}
Apparently Firefox and IE very vaguely think I'm doing cross site scripting ... on localhost ?
For the URL I've tried passing absolute and relative paths:
document.getElementsByTagName('base')[0].href+'standard/'
window.location.href
'standard'
I've looked up a couple dozen pages most of which are here on Stack and the questions are all off.
I have zero intention of cross-site scripting.
I'm not changing the method
.
I'm not using a different protocol (all HTTP, not HTTPS).
I'm not using any subdomains.
I never rely on or weaken my code with frameworks.
I do not care if the URL must be absolute or relative, I just need it to work.
This is for an intranet website.
The server and client are both the same computer.
The originating URL is formatted as: http://localhost/Client/standard/?http-query
The target URL is http://localhost/Client/standard/
.
The Apache Access log shows that the request goes through and returns HTTP 200.
An alert()
immediately after open
None of the states for onreadystatechange
are logged in the console.
Not all parameters need to be set when calling the ajax
function.
What am I missing?
You're calling send()
twice for the same XHR object, that's probably where the error comes from, as that would trigger an InvalidStateError
as the object is not open anymore, it's already sending.
function ajax(method, url, param_id_container_pos, id_container, id_focus, sequel) {
....
if (method == 'post') {
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xhr.send(param_id_container_pos); // you're sending here
}
xhr.send(null); // and then again here, which triggers an error
..........
Also, you should generally url encode the data you're sending
var data = 'part=' + encodeURIComponent(p) +'&price=' + encodeURIComponent(q);
ajax('post', 'standard/', data, 'sequel_sub_object_key');
All together
var data = 'part=' + encodeURIComponent(p) +'&price=' + encodeURIComponent(q);
ajax('post', 'standard/', data, 'sequel_sub_object_key');
function ajax(method, url, param_id_container_pos, id_container, id_focus, sequel) {
var methods = ['get', 'head', 'post'];
if (window.XMLHttpRequest && methods.indexOf(method) != -1) {
var xhr = new XMLHttpRequest();
xhr.open(method, url, true);
if (method == 'post') {
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xhr.send(param_id_container_pos);
} else {
xhr.send(null);
}
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && xhr.status == 200) {
alert('xhr.responseText = ' + xhr.responseText);
}
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.