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.