I tried to call a asp.net controller method with a json string/object from javascript.
The asp.net controller:
public class HomeController : Controller
{
public ActionResult doWork(string data) {
// dowork..
return new EmptyResult();
}
}
And in the javascript is:
var XHR=new XMLHttpRequest();
XHR.open("GET", 'http://localhost:58476/home/dowork', true);
XHR.setRequestHeader("Accept","application/json");
XHR.onreadystatechange = function () {
if (XHR.readyState == 4 && XHR.status == 200) {
alert('ok');
}else{
alert('not ok');
}
};
XHR.send(JSON.stringify(queryResult));
If the javascripts run it will call dowork method in asp.net, but data is null. And in de onreadystatechange it is calling the 'not ok'-alert.
In my console log I found the error:
XMLHttpRequest cannot load http://localhost:58476/home/dowork. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8081' is therefore not allowed access.
Does somebody know how to fix this?