Use xhr code error on switch/case (javascript) [closed] - javascript

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I'm trying to display some modal depending on the xhr error code, but I don't know why my switch is failing:
error: function (xhr) {
var codigo_error = parseInt(xhr.status);
console.log("codigo_error: " + codigo_error);//this shows 404
switch (codigo_error){
case 404: body = "Error 404";
default: body = "Other";
}
modal(body);
}
I always get the default case but in console.log I can see the 404. i tried with case '404' but there's no difference.
And if I put if(codigo_error == 404) alert(codigo_error); I can see the alert with 404

I always use a break in my switch cases.
switch(codigo_error){
case 404:
body = "Error 404";
break;
default:
body = "Other";
break;
}
modal(body);
Try that and see if it works.

Related

EmailJS message can't be sent [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 23 hours ago.
Improve this question
I'm new to EmailJS and trying to use it on my portfolio in the contact form. I tried different solutions and still showing the same error, I think I'm stuck now
I created the account, activated it, activated MFA as well, and tried multiple codes from the doc of EmailJS and from tutorials and still showing me the same error on the console. I even changed the service_id, template_id, and public key.
The error: Uncaught The public key is required. Visit https://dashboard.emailjs.com/admin/account
code on HTML:
<script type="text/javascript"
src="https://cdn.jsdelivr.net/npm/#emailjs/browser#3/dist/email.min.js">
</script>
<script type="text/javascript">
(function(){
emailjs.init("My-Public-Key");
});
</script>
code on JS:
function SendMail(){ event.preventDefault();
var params ={
from_name : document.getElementById("fullname").value,
email_id : document.getElementById("email-id").value,
message : document.getElementById("message").value
}
const serviceID = "service_uzy3k87";
const templateID = "template_btcihm8";
emailjs.send(serviceID, templateID, params)
.then(res=>{
document.getElementById("fullname").value = "";
document.getElementById("email-id").value = "";
document.getElementById("message").value = "";
console.log(res);
alert("Your message sent successfully!!")
})
.catch(err=>console.log(err));
}

If else displaying wrong answer when using fetch [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
So what I'm trying to do is create an if/else statement, where if your IP is from Japan the console log will show "You are in Japan", and if your IP is from any other country the console log will show "I guess you are in the USA". The only problem is, it doesn't work. Even when I use VPN to pretend I am from Japan, the console log still displays "I guess you are in the USA".
This is the code I currently have:
async function country() {
let response = await fetch("https://ipinfo.io/json?token=8a21f471f4e583")
let CountryResponse = await response.json()
console.log(CountryResponse)
if (CountryResponse == "JP") {
console.log('You are in Japan');
} else {
console.log('I guess you are in the USA');
}
}
country()
So basically I would just like for the console log to show "You are in Japan" if the IP is from Japan, anything else the console log can show "I guess you are in the USA".
if (CountryResponse.country === "JP") {
console.log('You are in Japan');
} else {
console.log('I guess you are in the USA');
}
The api you are calling does not return only the county code but a object witch contains this information

Javascript Discord Bot 'SyntaxError: missing ) after argument list' [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I've been trying to create a discord bot that filters certain words and then sends a message afterwards. Here is my current code:
const Discord = require('discord.js');
const client = new Discord.Client();
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});
//runs the message looker thingy
client.on('message', async message function {
//1 blacklisted words
let blacklisted = ['communism', 'fascism', 'socialism', 'conservatism', 'racism', 'sexism', 'nazism', 'marxism'] //words put , after the word
//2 looking for words
let foundInText = false;
for (var i in blacklisted) { // loops through the blacklisted list
if (message.content.toLowerCase().includes(blacklisted[i].toLowerCase())) foundInText = true;
}
// checks casesensitive words
//3 deletes and send message
if (foundInText) {
message.delete();
client.message.send('ISM SPOTTED. OFFICIAL ISM REPORT HAS BEEN FILED AND WILL BE OVERLOOKED BY THE HIGH ISM COUNCIL. ANYTHING YOU SAY CAN AND WILL BE USED AGAINST YOU IN A COURT OF LAW. YOUR FATE SHALL BE DETERMINED.');
}
});
However I keep experiencing an error saying:
client.on('message', async message function {
^^^^^
SyntaxError: missing ) after argument list
I have placed an end bracket everywhere in the line of code and it still doesn't work. Can someone please explain the error and tell me what to do to fix it?
A function definition needs to have parentheses for its arguments, even if there are none.
function(){}

SyntaxError: Unexpected token [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
For some reason, Unexpected token keeps getting returned. Any thoughts on how to fix it?
var sleepCheck = function(numHours);
if (numHours>=8)
{
return "You're getting plenty of sleep! Maybe even too much!";
}
else
{
return "Get some more shut eye!";
}
Function in javascript have its body delimites by { and }:
var sleepCheck = function(numHours) { // <-- START HERE
if (numHours>=8)
{
return "You're getting plenty of sleep! Maybe even too much!";
}
else
{
return "Get some more shut eye!";
}
}; // <-- END HERE
Your syntax is just wrong. Try this instead:
var sleepCheck = function(numHours)
{
if (numHours>=8)
{
return "You're getting plenty of sleep! Maybe even too much!";
}
else
{
return "Get some more shut eye!";
}
};

Using URL fragment to determine which jquery function is run [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want to link to a page like (domain.com/page#loadThickBox1) and have it run jquery function that loads a thickbox by default. What code do I need to run to accomplish this?
For instance, these might be a few links:
domain.com/page#loadThickBox1
domain.com/page#loadThickBox2
domain.com/page#loadThickBox3
NOT like this:
domain.com/page/loadThickBox1
domain.com/page/loadThickBox2
domain.com/page/loadThickBox3
Use location.hash
$(function() {
switch( location.hash.replace('#','') ){
case 'loadThickBox1':
//do something!
loadThickBox1();
break;
case 'loadThickBox2':
//do something!
loadThickBox2();
break;
case 'loadThickBox3':
//do something!
loadThickBox3();
break;
}
});
location.href.split('#')[1]
This will get you the words after the #. Load the appropriate box given this value.
You can listen to the hashchange event, parse the hash and run your function accordingly.
DEMO
function handleHash(hash) {
var hash = hash.replace('#', '');
if (!hash) return;
console.log('Loading thick box ' + hash.slice(-1));
}
window.addEventListener('hashchange', function () {
handleHash(location.hash);
});
//handle initial hash when page loads
handleHash(location.hash);

Categories

Resources