您好我正在创建一个异步WCF服务并在HTML应用程序中使用它,但每次我从服务中获取null。我是异步方法的新手,所以请指导我,我在这里分享我的代码:
Service.cs
public static async Task<string> GetTJSON()
{
string responseData = string.Empty;
using (var httpClient = new HttpClient())
{
using (var response = await httpClient.GetAsync("http://api.xyz.com/json/feeds?system=apios").ConfigureAwait(false))
{
responseData = await response.Content.ReadAsStringAsync();
return responseData;
}
}
}
public async Task<string> getJ()
{
var json = await GetTJSON();
return json.ToString();
}
Iservice.cs
[OperationContract]
[WebInvoke(Method = "GET", UriTemplate = "GetJsonNet1", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]
Task<string> getJ();
HTML / Jquery的
var GetJsonNet = function () {
$.ajax({
type: "GET",
url: "http://localhost:59835/Service1.svc/GetJsonNet1",
contentType: "application/json;charset=utf-8",
dataType: "json",
success: function (data) {
var result = data.GetEmployeeJSONResult;
console.log(result);
var obj = $.parseJSON(result);
alert('s:' + obj);
},
error: function (xhr) {
alert('e:'+xhr.responseText);
}
});
}