Here is my controller in server:
public class ChatController : ApiController
{
[HttpGet]
public HttpResponseMessage HelloWorld()
{
string result = "<h1>Hello world! Time is: " + DateTime.Now + "</h1>";
var resp = new HttpResponseMessage(HttpStatusCode.OK);
resp.Content = new StringContent(result, Encoding.UTF8, "text/plain");
return resp;
}
}
Here is my JavaScript code:
$.ajax({
type: "GET",
url: "http://localhost:8080/api/Chat/HelloWorld",
cache: false,
contentType: "text/plain",
success: function (data) {
alert(data);
},
error: function (error) {
alert('error');
}
});
It always shows 'error'. What's wrong? By the way, when I copy the url in browser's address bar, the browser can show the correct message from the web api.