I'm currently refactoring some of my previous code to move away from jQuery's AJAX function towards XMLHttpRequest in vanilla JS. From my understanding, the following code blocks should be identical. However, while the jQuery version works, XMLHttpRequest doesn't. If successful, you should see an array returned by PHP in the network tab in dev tools.
jQuery
$("#submit").click(() => {
$.ajax({
type: "POST",
url: "http://arig.asuscomm.com/mn/PHP/submitNames.php",
data: {
first: "Hi!"
}
})
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button id="submit">
Submit
</button>
Vanilla JS
function send() {
var data = {
"first": "Hi!"
};
var xhr = new XMLHttpRequest();
xhr.open("POST", "http://arig.asuscomm.com/mn/PHP/submitNames.php", true);
xhr.setRequestHeader('Content-Type', 'application/json;charset=UTF-8');
xhr.send(JSON.stringify(data))
}
<button id="submit" onclick="send()">
Submit
</button>
Note that my server is running locally, but I will try to keep it up until my question is answered. And, because my server doesn't have HTTPS quite yet, you might need to send the request from an HTTP source. Thanks!!!
Edit: I'm using code found here.