Bag*_*vas 5 javascript php ajax jquery
我在google上搜索过,关于stackoverflow的这个话题有很多问题.例如"数据不是通过邮寄方法发送"等等.但似乎不是我的问题
案件与其他问题几乎相同.这些是错误消息:
Firefox(第21版):
InvalidStateError: An attempt was made to use an object that is not, or is no longer, usable.
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
Run Code Online (Sandbox Code Playgroud)
Chrome(第27版):
Uncaught Error: InvalidStateError: DOM Exception 11
Run Code Online (Sandbox Code Playgroud)
发送请求时GET,没有错误.并且所有GET数据都很好.
但是当由POST+ 发送时setRequestHeader,itu会发生如上所述的错误.当setRequestHeader删除,错误消失.没有错误,但没有收到POST数据.我print_r($_POST); 那么数组是空的
问题已更新.这是来电者:
goServer({
url : 'users/sign-in.php',
method : 'POST',
data : 'randomId=' + randomId + '&name=' + name + '&password=' + password,
done : function(response) {
alert(response);
},
fail : function(response) {
alert(response);
}
});
Run Code Online (Sandbox Code Playgroud)
这是功能(对不起,长行):
function randomString() {
var str = new Date().getTime(),
str2 = Math.random();
str = str.toString();
str2 = str2.toString();
str2 = str2.substring(2,7);
str += str2;
return str;
}
function goServer(opts) {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = requestComplete;
function requestComplete() {
if ( xhr.readyState === 4 ) {
if ( xhr.status === 200 ) {
opts.done(xhr.responseText);
} else {
opts.fail(xhr.responseText);
}
}
}
if ( !opts.method || opts.method === undefined ) {
opts.method = "GET";
}
if ( !opts.cache || opts.cache === undefined ) {
opts.cache = false;
}
if ( opts.cache === false ) {
opts.url += '?nocache=' + randomString();
} else {
opts.url += '?nocache=false';
}
if ( opts.method === "GET" ) {
if ( opts.data !== '' && opts.data !== undefined ) {
opts.url += '&' + opts.data;
}
opts.data = '';
} else {
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
}
xhr.open(opts.method, opts.url, true);
xhr.send(opts.data);
}
Run Code Online (Sandbox Code Playgroud)
注意,数据参数(opts.data)在GET发送时设置为url.当通过POST发送时,参数设置为xhr.send(opts.data);
问题:如何正确获取POST数据?
谢谢
Mus*_*usa 12
打电话xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');后打电话xhr.open
也opts.data应该是包含键/值对的字符串.例如key=value&method=post
| 归档时间: |
|
| 查看次数: |
3167 次 |
| 最近记录: |