How to enable cross origin queries in MVC 6 web api

I wanted to access my web api using jsonp to bypass scripts with multiple sites.

[HttpGet("{email}")]        
public User 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 which

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);
    }
});

How do I get below the error

"parsererror" errorThrown Error: jQuery110206458149312522913_1441780598078 was not called

What is wrong here? How to configure jsonp return formatted result on web api? or do you need to allow cross-domain from code to deal with it? are there any settings i need to do. While doing google, I found that JsonpMediaTypeFormatter can be used to get data. How can I install this in MVC 6 web api.

There seems to be some more problems with mvc 6 web api, I will find out that after that I will add this.

+4
1

Produces?

, Produces .

Method level ( , , Controller level).

[HttpGet("{email}")]
[Produces("application/jsonp")]        
public Customer Get(string email)
{
...
}
+3

Source: https://habr.com/ru/post/1606536/


All Articles