Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

Enable cross-origin requests in web api MVC 6

$
0
0

I wanted to access my web api from out side using jsonp to bypass the cross site scripting.

[HttpGet("{email}")]        
public Customer Get(string email)
{
    var user = (from usr in db.User
            join co in db.UserDetails on usr.id equals co.userId
            where co.email.Equals(email) || usr.email.Equals(email)
            select usr).FirstOrDefault();

    return user;
}

Here is my javascript code that

jQuery.ajax({
    type: "GET",
    url: "http://localhost:54381/api/userapi/test1@test.com",
    dataType: "jsonp",
    success: function (response) {
       var t = JSON.parse(response);
       alert(t.name);
    },
    error: function (jqXHR, textStatus, errorThrown) {
      alert("Error" + textStatus + "      " + errorThrown);
    }
});

As I am getting below error

"parsererror" errorThrown Error: jQuery110206458149312522913_1441780598078 was not called

What is wrong here? How to set to web api return jsonp formatted result? or have to enable cross domain from code to deal with it? is there any setting that I need to do. Doing google I found that JsonpMediaTypeFormatter can be used to get data. How I can set this in MVC 6 web api.

Seems there is some more problems with the mvc 6 web api, I'm figuring out after that I will add here that.

Update: MVC 6 WebAPI problem:

I have deployed my mvc6 application on Windows Server 2012 R2 and IIS version (8.5.9600.16384). When I deployed my code and call the web api it works perfect and return data, and when next day I open the web api I'm getting

404 - File or directory not found.

Why this is happening?


Viewing all articles
Browse latest Browse all 9386

Trending Articles