1

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?

2 Answers 2

2

One easy solution would be to add the Access-Control-Allow-Origin.
You can add this to your web.config file like:

<system.webServer>
    <httpProtocol>
      <customHeaders>
        <add name="Access-Control-Allow-Origin" value="*" />
      </customHeaders>
    </httpProtocol>
</system.webServer>

Also, you can send data only on POST request, you're doing a GET request. More info on send method check w3schools
This means that you have to change your XHR.Open method to XHR.open("POST", 'localhost:58476/home/dowork', true);

Sign up to request clarification or add additional context in comments.

6 Comments

The 'ok' alert is shown. but the data in in the method dowork is still null. How can if fix this?
is queryResult containing any data?
Yes it is!, i have also tried to do var data = 'someDate'; XHR.send(JSON.stringify(data)); but in dowork() data is still null.
Also, you can send data only on POST request, you're doing a GET request. More info on send method check w3schools
i have also chang it to a httpPost reqest and in the aps.net [HttpPost] but still data = null
|
1

Obviously , The javascript code is sending a request to a different server to the one where the page was originally loaded from.

The first server is listening on port 8081 , and the other one is listening on port 58476.

Due to the same-origin policy , you can't send AJAX requests to any server other than the server the JS script is hosted on.

while you can turn off this security feature , it is not recommended to turn it off unless you keep in mind that anyone can inject a Javascript code in your page and make it send requests , which might contain sensitive data like authentication cookies , to their server .

1 Comment

So i cannot do httprequest from 8081 to another port? if so, how do I turn off this security feature?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.