简体   繁体   中英

Ajax call to Asp.net WebApi not works

I making the simplest ajax call to the WebApi Asp.Net, but does not working problem is unknown

I tried all solutions on StackOverFlow and other forums, I even found similar peoples that had similar questions but there is no answer.

<form id="form2">
<div>
    <label for="status">Status</label>
</div>
<div>
    <input id="status1" type="text" />
</div>
<div>
    <input type="submit" value="Submit" />
</div>
<span id="displayText"></span>

html file

$('#form2').submit(function () {
    $.ajax({
        url: "/api/values/",
        method: "get",
    }).done(function (data) {
        $("#displayText").text(data);
    });
});

ajax call

[EnableCors(origins: "*", headers: "*", methods: "*")]
public class ValuesController : ApiController
{

    // GET api/values
    public IEnumerable<string> Get()
    {
        return new string[] { "value1", "value2" };
    }
}

my controller

Do I need to install something on my damn PC to make ajax working?

The problem was because

[Deprecated] Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience.

I changed async method

async: true

in ajax call

$.ajax({
    url: "/api/values/5",
    method: "get",
    error: function (request, status, error) {
       console.log(alert(request.responseText));
     },
    async: true
}); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM