gpt4 book ai didi

javascript - 将原型(prototype) Ajax POST 重建为 "plain"js

转载 作者:行者123 更新时间:2023-12-02 22:33:42 25 4
gpt4 key购买 nike

我需要的是通过javascript重建prototype.js Ajax请求。 (没有 jQuery/没有获取)。需要 IE 支持(由于某些原因......)

new Ajax.Request(
urlVar,
{
method: 'post',
parameters: {elid: document.getElementById('element').selectedIndex},
onCreate: function(){
//do somethings
},
onComplete: fucnction (event){
currentValues = eval('(' + event.responseText + ')');
console.log(currentValues); // will give me an array with 3 objects
}
});

我尝试过的是:

var xhr = new XMLHttpRequest();
xhr.open("POST", urlVar, true);
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhr.onreadystatechange = function (event) {
if (xhr.readyState == 4) {
if (xhr.status == 200) {
currentValues = eval('(' + event.responseText + ')');
console.log(currentValues); // i see an error that "currentValues" is undefined
//i can get 1 object if i will do eval('(' + THIS.responseText + ')');
// but it still bugs me a lot..

//other actions here
}
else if (xhr.status == 400) {
console.log('ERROR')
}
else {
console.log('something else other than 200 was returned');
}
}
}
xhr.send(document.getElementById('element').selectedIndex);

也许有人知道在这种情况下哪种结构适用。有什么方法可以通过 js 执行 onCreate/onComplete 吗?

最佳答案

使用 this.responseTextxhr.responseText(与您访问 .status.readyState 的方式相同) code>) 或 event.target.responseText事件本身没有responseText

关于javascript - 将原型(prototype) Ajax POST 重建为 "plain"js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58808149/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com