Dismiss
Announcing Stack Overflow Documentation

We started with Q&A. Technical documentation is next, and we need your help.

Whether you're a beginner or an experienced developer, you can contribute.

Sign up and start helping → Learn more about Documentation →

I wanted to send mobile number and emailID using AJAX and jQuery to my MVC controller to check whether these details are already exist in DB or not.

When I am using [HttpPost] attribute on my action method then I am getting error as :- "Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at url.

If I remove [HttpPost] attribute then my action method is getting called but all the values received in action method argument are null.

below is the action method code.

        public class LogOnModel
    {
        public string Mobile { get; set; }
        public string EmailID { get; set; }

    }

    [HttpPost]
    [AllowAnonymous]
    ///AJAXService/LogOnAjax
    public ActionResult LogOnAjax(LogOnModel obj)
    {
        return Json(true);

    }

Below is my AJAX call.

       var LoginModel = {
        'Mobile': "ASH",
        'EmailID': "MITTAL",

    };
    $.ajax({
        url: 'http://localhost:51603/AJAXService/LogOnAjax',
        type: 'GET',
        contentType: 'application/json',
        dataType: 'jsonp',
        data: JSON.stringify(LoginModel),
        success: function (result) {
            if (result == true) {
                window.location = "/Dashboard";
            } else {
                $QuickLoginErrors.text(result);
            }
        }
    });

I have place the below code in my web.config file to avoid CORS errors.

 <customHeaders>
    <add name="Access-Control-Allow-Origin" value="*" />
    <add name="Access-Control-Allow-Headers" value="Content-Type" />
    <add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
  </customHeaders>

please help how to call action method using AJAX to post/Get some data I am new to ASP.NET MVC and struggling a lot to solve this.

share|improve this question
    
I wouldn't expect a CORS error if you are posting to your own website. Can you check that the URL correct? – Karl Gjertsen Nov 6 '15 at 15:13
    
you see this? stackoverflow.com/questions/6290053/… – JamieD77 Nov 6 '15 at 16:51
up vote 2 down vote accepted

if the AjaxService controller is in the same project as the View you're working on.

var LoginModel = {
    Mobile: "ASH",
    EmailID: "MITTAL"
};

$.ajax({
    url: '@Url.Action("LogOnAjax","AJAXService")', // try to use Url Helper when possible
    type: 'POST', // use Get for [HttpGet] action or POST for [HttpPost]
    //contentType: 'application/json', not needed
    //dataType: 'jsonp', jsonp is for sending to a site other than the current one.. 
    data: LoginModel, // no need to stringify
    success: function (result) {
        if (result == true) {
            window.location = "/Dashboard";
        } else {
            $QuickLoginErrors.text(result);
        }
    }
});
share|improve this answer
    
No I have just created this html code in a simple test.html file, and then opening this html file via Chrome browser. I don't have this code in my view.. – Ashish Mittal Nov 6 '15 at 16:43
    
Thanks a lot Jamie... I just added that AJAX call inside my view and everything started working after that..... Many thanks again. – Ashish Mittal Nov 6 '15 at 17:55

Keep the [HttpPost] and add this line right before your $.ajax line:

$.support.cors = true;

Also change your $.ajax method to match your [HttpPost]. Change the line:

type: 'GET',

to

method: 'POST',

Note: This only worked in IE for me. Chrome still refused to complete the Ajax call. However, that was a while back and I'm not sure if the newer versions of Chrome will work with this line.

Very important note: Only use this line in development. Do not compile your App with this line.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.