TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Pradeep Yadav
320
5.7k
2.7m
Access to XMLHttpRequest has been blocked by CORS policy
Nov 16 2018 4:36 AM
Hi,
I have created Web Api and trying to access it from MVC application,
Now Web Api is having below method
[HttpGet]
[Route("api/UserMasterApi/GetUser")]
[EnableCors("*", "*", "*")]
public HttpResponseMessage GetUser(string userName, string userPassword)
{
HttpResponseMessage responseMsg = new HttpResponseMessage();
var user = new UserMastersDBRepository().GetUser(
new UsersMasterBusinessModel()
{
UserName = userName,
UserPassword = userPassword
}
);
if (user == null)
{
return
responseMsg =
Request.CreateResponse(HttpStatusCode.OK,
new
{
Status = "Fail",
Message = "Get Api",
Body = new { Username = "" }
});
}
else
{
return
responseMsg =
Request.CreateResponse(HttpStatusCode.OK,
new
{
Status = "SUCESS",
Message = "Get Api",
Body = new { Username = user }
});
}
}
Now when I am trying to access this using ajax call
$.ajax({
//url: 'http://localhost:64995/api/UserMasterApi/',
url: 'http://localhost:64995/api/UserMasterApi/GetUser/',
contentType: 'application/json',
// dataType: 'json',
accepts: "application/json; charset=utf-8",
type: 'POST',
data: JSON.stringify({ userName: userName, userPassword: password }),
success: function (data) {
debugger
if (data.Success) {
window.location.href = loc + "/Home";
}
});
And in WebApiConfig
public static void Register(HttpConfiguration config)
{
config.EnableCors();
// Web API configuration and services
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
Getting error for CORS.
Please help.
Reply
Answers (
3
)
Difference between single and double quotes in Javascript?
best kind of CSS