简体   繁体   English

在javascript中发出http请求时出现400错误

[英]400 error while making http request in javascript

I ctrl+c the part of Max Payne Wiki article(it's just an exmaple of any text):我 ctrl+c 是 Max Payne Wiki 文章的一部分(它只是任何文本的一个例子):

just a screenshot of how I copy part of the article只是我如何复制部分文章的屏幕截图

Then I ctrl+v this stuff into <textarea> in my site(not in the code, but literally in the site-rendered <textarea> )然后我 ctrl+v 把这些东西放到我网站的<textarea>中(不是在代码中,而是在网站渲染的<textarea>

The the runs the next javascript code:运行下一个 javascript 代码:

SomeParagraphElement.innerText=document.getElementById('my_txtarea').value;
requestp("aga.php?data="+SomeParagraphElement.innerText, callback_function);

where requestp is requestp在哪里

function requestp(path, run)
{
var request = new XMLHttpRequest();
request.open('GET', path, true); 

request.addEventListener('readystatechange' ,function()
{
if ((request.readyState==4) && (request.status==200))
run( request.responseText);
}
);

request.send(null);

}

After this there should be a data uploaded to sever, but it doesn't happen and if I just write stuff from keyboard by my fingers and even insert emojis - all works fine.在此之后应该有一个数据上传到服务器,但它不会发生,如果我只是用手指从键盘写东西甚至插入表情符号 - 一切正常。

Google Chrome debug window says, that I make a 400 HTTP error.谷歌浏览器调试窗口说,我犯了一个 400 HTTP 错误。 I tried我试过

var str= SomeParagraphElement.innerHTML.replace(/(?:\\r\\n|\\r|\\n)/g, '%0A'); and another %blahblah symbol, but it doesn't change anything.和另一个 %blahblah 符号,但它不会改变任何东西。

If I make a new line by myself by pressing 'enter', via keyboard it works fine.如果我通过按“回车”自己创建一个新行,则通过键盘它可以正常工作。

What should I do?我该怎么办?

Yeah, if I pass data more the 2048 bytes, I have to use POST request.是的,如果我传递的数据超过 2048 字节,我必须使用 POST 请求。 Here is a function, maybe it will be helpfull for someone这是一个功能,也许对某人有帮助

function requestp(path, data, callback)
{
var request = new XMLHttpRequest();
request.open('POST', path, true); 
request.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');//specify this!

request.addEventListener('readystatechange' ,function()
{
if ((request.readyState==4) && (request.status==200))
callback( request.responseText);
}
);

request.send(data);

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM