Reputation: 713
$.ajax({
type : 'POST', // define the type of HTTP verb we want to use (POST for our form)
url : 'https://your_url.aspx', // the url where we want to POST
data : school, // our data object
dataType : 'json', // what type of data do we expect back from the server
encode : true
})
// using the done promise callback
.done(function(data) {
// log data to the console so we can see
console.log(data);
// here we will handle errors and validation messages
});
// stop the form from submitting the normal way and refreshing the page
event.preventDefault();
This is my code to post some form data to a specific url, but I am getting the following error:
POST https://your_url.aspx 500 (Internal Server Error)
send @ jquery-3.2.1.min.js:4
ajax @ jquery-3.2.1.min.js:4
(anonymous) @ magic.js:20
dispatch @ jquery-3.2.1.min.js:3
q.handle @ jquery-3.2.1.min.js:3
index.html:1 XMLHttpRequest cannot load
https://your_url.aspx. No 'Access-Control-
Allow-Origin' header is present on the requested resource. Origin 'null' is
therefore not allowed access. The response had HTTP status code 500.
I looked around for how to allow access-control-allow-origin and used in $.ajax
beforeSend: function(request){
request.setHeaderRequest("Access-Control-Allow-Origin","*")
}
also later on after seeing more responses in stackoverflow I changed setHeaderRequest to addHeaderReuest, but even then I did not get to see the response from the server, although the errors were removed. The console appeared blank.
Any help would be appreciated.
Upvotes: 2
Views: 979
Reputation: 6004
This error came because you are trying to hit another domain from your domain. Using the Same Origin Policy, we can request only those url's which exists in the same domain. The cross domain request generally known as CORS is not allowed by browser until we have an authentication from another site. The Access-Control-Allow-Origin response header indicates whether the response can be shared with resources with the given origin. check the documention Access-Control-Allow-Origin
Upvotes: 1