Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

JavaScript Unable to get a Json back from a request

TLaperre

New Coder
I have a endpoind that works. Get and post work from Postman. When using the postman proxy, it tells me, success and indeed, the expecterd resuls are seen there. Trying to get the results back in VScode, I have been looking for 3days sync/async nothing is working beside a status=4. A bit of help is welcome.

ex1: Works in postman:
Post: http://davincironse.com:3000/Sms
Body:
{
"mySms": {},
"smsType": 0,
"mobileNumbers": "0479123456",
"message": "testing104",
"method": 0,
"interface": 1,
"priority": 0,
"quantity": "string"
}

ex2: Works in postman:
Post: http://davincironse.com:3000/WeatherForecast
Body:

For ex1 call javasript not working:
export async function sendSmsMessage3() {
var enpointurl = "http://davincironse.com:3000/Sms";
var messageSmsDetails =
{
mySms: {},
smsType: 0,
mobileNumbers: '0479536456',
message: 'testing104',
method: 0,
interface: 1,
priority: 0,
quantity: 'string'
};


var strBody = JSON.stringify(messageSmsDetails);
var url = "http://www.davincironse.com:3000/Sms";
// url = "http://127.0.0.1:5112/Sms";
console.log(document.referrer);
try {
const response = await fetch(url, {
method: 'POST',
headers: { 'Content-type': 'application/json; charset=UTF-8' },
body: strBody // JSON.stringify(messageSmsDetails)'
});
if (!response.ok) {
throw new Error(HTTP error! status: ${response.status});
}
const data = await response.json();
console.log(data);
} catch (error) {
console.error('Error:', error);
throw error;
}

};

For ex2 call javascript not working:
export async function sendSmsMessage8() {
const xhr = new XMLHttpRequest();
xhr.open("GET", "http://davincironse.com:3000/WeatherForecast", true);
xhr.onload = (e) => {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
console.log(xhr.responseText);
} else {
console.error(xhr.statusText);
}
}
};
xhr.onerror = (e) => {
console.error(xhr.statusText);
};

xhr.send(null);
};
 
Can you provide:
  1. The exact error message you're getting?
  2. The network tab information from the failed request?
  3. Whether you see the request hitting your server logs?

Common Issue:
CORS (Cross-Origin Resource Sharing)

Postman doesn't enforce CORS, but browsers do

Check if the server needs to include proper CORS headers
 
Last edited:

Buy us a coffee!

Buy me a coffee.
Back
Top Bottom