Im trying to send HTML form data to 2 discord webhooks but it will only ever send to the second one. It displays no error messages or anything but it just doesn't send to the second webhook.
const request = new XMLHttpRequest();
request.open("POST", "https://discord.com/api/webhooks/920367877704523816/LpIHlNUfEsd0_lG5DWVL7zvCTSZQewmXYQprHnWpWNBESnVJnvVYF3JG9-SCZCzTpLcl");
request.open("POST", "https://discord.com/api/webhooks/919233892538540103/C75vznMMpgv2qqecF5Tmjw7C1xcLFlVSrMuCvdZ6Zg4jUsJrFkLSg6--ZRMN_lN809yp");
request.setRequestHeader('Content-type', 'application/json');
const params etc...
request.send(JSON.stringify(params));
CodePudding user response:
you need to create 2 separate request objects, you set a post url to the same request twice so it's taking the last one assigned
const request = new XMLHttpRequest();
request.open("POST", "https://discord.com/api/webhooks/920367877704523816/LpIHlNUfEsd0_lG5DWVL7zvCTSZQewmXYQprHnWpWNBESnVJnvVYF3JG9-SCZCzTpLcl");
const request2 = new XMLHttpRequest();
request.open("POST", "https://discord.com/api/webhooks/920367877704523816/LpIHlNUfEsd0_lG5DWVL7zvCTSZQewmXYQprHnWpWNBESnVJnvVYF3JG9-SCZCzTpLcl");
request2.open("POST", "https://discord.com/api/webhooks/919233892538540103/C75vznMMpgv2qqecF5Tmjw7C1xcLFlVSrMuCvdZ6Zg4jUsJrFkLSg6--ZRMN_lN809yp");
request.setRequestHeader('Content-type', 'application/json');
request2.setRequestHeader('Content-type', 'application/json');
const params etc...
request.send(JSON.stringify(params));
request2.send(JSON.stringify(params));
you can also move your urls to an array and iterate it like this
["https://discord.com/api/webhooks/920367877704523816/LpIHlNUfEsd0_lG5DWVL7zvCTSZQewmXYQprHnWpWNBESnVJnvVYF3JG9-SCZCzTpLcl", "https://discord.com/api/webhooks/919233892538540103/C75vznMMpgv2qqecF5Tmjw7C1xcLFlVSrMuCvdZ6Zg4jUsJrFkLSg6--ZRMN_lN809yp"].forEach((url) => {
const request = new XMLHttpRequest();
request.open("POST", url);
request.setRequestHeader('Content-type', 'application/json');
const params etc...
request.send(JSON.stringify(params));
})
CodePudding user response:
Maybe you want two requests instead of redefining the one you have.
const requestDiscordOne = new XMLHttpRequest();
requestDiscordOne.open("POST", "https://discord.com/api/webhooks/920367877704523816/LpIHlNUfEsd0_lG5DWVL7zvCTSZQewmXYQprHnWpWNBESnVJnvVYF3JG9-SCZCzTpLcl");
requestDiscordOne.setRequestHeader('Content-type', 'application/json');
const params etc...
requestDiscordOne.send(JSON.stringify(params));
const requestDiscordTwo = new XMLHttpRequest();
requestDiscordTwo.open("POST", "https://discord.com/api/webhooks/919233892538540103/C75vznMMpgv2qqecF5Tmjw7C1xcLFlVSrMuCvdZ6Zg4jUsJrFkLSg6--ZRMN_lN809yp");
requestDiscordTwo.setRequestHeader('Content-type', 'application/json');
const params etc...
requestDiscordTwo.send(JSON.stringify(params));
Obviously optimizing for reusability and stuff, but hope you get the idea.
Have a nice day!
CodePudding user response:
Like others have already stated, the second open
call re-initializes the request.
But do yourself a favour and use the Fetch API for HTTP requests. It's cleaner and much easier to use. Especially in combination with async/await
.
async function getSomething() {
const response = await fetch('/someApiUrl');
// Waits until the request completes...
console.log(response);
}