I've been attempting to make a POST request using Ajax, but I keep encountering an error with status code 0. Strangely, all the request parameters seem to be functioning correctly in the Advanced REST Client.
Here's My Code Snippet:
<button>Post</button>
$('button').click(function(e) {
e.preventDefault();
$.ajax({
url: "https://api.sendgrid.com/v3/mail/send",
type: "POST",
contentType: 'application/json',
headers: {
'Authorization' : 'Bearer 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',
'Content-Type': 'application/json'
},
data: JSON.stringify({
"personalizations": [{
"to": [{
"email": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a2d6c7d1d6e2c7dac3cfd2cec78cc1cdcf">[email protected]</a>"
}]
}],
"from": {
"email": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7f1e1d1c1b3f18121e1613511c1012">[email protected]</a>"
},
"subject": "Great, World!",
"content": [{
"type": "text/plain",
"value": "Cool OK!"
}]
}),
dataType: "json",
success: function (response) {
var resp = JSON.parse(response)
alert(resp.status);
},
error: function (xhr, status) {
alert("error : "+status+" :: "+JSON.stringify(xhr));
}
});
});
Despite my best efforts, the code above continues to throw an error:
{"readyState":0,"responseText":"","status":0,"statusText":"error"}
You can also view this issue on jsfiddle
I'm in need of assistance to resolve this issue. If you have any suggestions or solutions, please help me out as I'm unable to identify the problem in the code.