I've read many of the "solutions" about 'POST requests on different domain' issue which is also known as 'Cross domain post', and still I cant make that works like they are saying that they fixed it.
From reading all of those articles about this issue I know now why that's happening, but I still need it to work in my project, in the secure way without using hack moves or such things. I would like if anyone knows how to make it works, to help us out, I mean 'us' because I believe that still there are people who didn't make it up.
My situation: I have webapp which is hosted on domainA, that makes ajax post call to domainB(mvc4 webAPI). ajax post call looks like this:
var profile = { Id:"1234567890", Name:"Name_01"}
$.ajax({
type: "POST",
url: 'http://domainB/api/Profile',
data: JSON.stringify(profile)
});
@domainB I used to set custom response headers:
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*"/>
<add name="Access-Control-Allow-Headers" value="X-Requested-With" />
<add name="Access-Control-Allow-Methods" value="POST, GET, OPTIONS"/>
</customHeaders>
</httpProtocol>
</system.webServer>
@domainB webAPI post method I have this:
public void Post(dynamic newProfile)
{
...
}
So on activated/started ajax call, in debugging mode at webAPI project I am getting SerializationException Operation could destabilize the runtime at JsonFX.Serilization.DataWriter in the webAPI, and as an error at browser I get
XMLHttpRequest cannot load http://domainA/api/Profile. Origin http://domainB is not allowed by Access-Control-Allow-Origin.
I am catching the requests and responds on Fiddler and I see that there are no Access-Control-Allow headers at all on the response side.
I wrote what I am using, now because this isn't worked out what should I do to make it happened that ajax post call working from domainA to domainB, or what is the real solution for this.
Rather than try to rely on various available scripts/workarounds to perform cross-domain calls why not create a web method on your own domain.
You can call this with your AJAX.
Then let your own web method perform the call to the other domain - this isn't subject to the same security issues as having your browser perform the call.
So your call would go something like this:
Browser AJAX Call --> Your own web method --> call to other domain service --> result returned to your method --> Your method returns back to your browser.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With