Cross Domain Webix Post
Recently, we were working with Spring Boot and Webix
framework using Ajax. We had quite some trouble to POST data back to the Server
with Spring Boot.
Error:
No 'Access-Control-Allow-Origin' header is present on the requested
resource.
Origin 'http://localhost:7100' is therefore not allowed access.
We realized the problem was because of our AJAX cross-domain
requests that are forbidden by default by the same-origin security policy.
Since browsers need to “preflight” any asynchronous or
rather HTTP request methods that modify data and also fetch an “approval” the
problem had to be addressed both way i.e. Server side and Browser
i)
We had to enable the CORS assertion in Spring
Boot i.e. @CrossOrigin(*)
ii)
Enable Cross-origin resource sharing (CORS) from
the Chrome browser. You need to add an extension or CORS enabler from the Chrome Web Store for this.
Thereafter using the following code the JSON data was post
back to the server successfully.
webix.ajax().headers({
"Content-type":"application/json"
}).post("./data.php", JSON.stringify(payload), function(text){
//optional
callback here
});
*** Do note, while enabling CORS resolves the problem but do
not forget to switch off CORS from browser post verification. Otherwise, that might
leave the site vulnerable to Cross Site Scripting issues (XSS) when the
external site is compromised.
Comments
Post a Comment