How do I send a cross-domain POST request via JavaScript?
Notes - it shouldn\'t refresh the page, and I need to grab and parse the response afterwards.
If you have access to the cross domain server and don't want to make any code changes on server side, you can use a library called - 'xdomain'.
How it works:
Step 1: server 1: include the xdomain library and configure the cross domain as a slave:
Step 2: on cross domain server, create a proxy.html file and include server 1 as a master:
proxy.html:
Step 3:
Now, you can make an AJAX call to the proxy.html as endpoint from server1. This is bypass the CORS request. The library internally uses iframe solution which works with Credentials and all possible methods: GET, POST etc.
Query ajax code:
$.ajax({
url: 'https://crossdomain_server/proxy.html',
type: "POST",
data: JSON.stringify(_data),
dataType: "json",
contentType: "application/json; charset=utf-8"
})
.done(_success)
.fail(_failed)