我有一个使用HttpClient并使用Javascript调用的VB.Net Web服务
<WebMethod()>
Public Async Function ADMIN_SearchGrantsJSON_projectNumber_Async(projectNumber As String) As Threading.Tasks.Task(Of String)
Dim term As String = projectNumber.Replace(" ", "")
Dim offset As Integer = 0
Dim retLimit As Integer = 50
Dim TotalCount As Integer = 0
Dim termName As String = "projectNumber:*" + term + "*"
Dim serverUrl As String = "https://api.federalreporter.nih.gov/"
Dim requestUrl As String = "/v1/projects/search?query=" & termName & "&offset=" & CStr(offset) & "&limit=" & CStr(retLimit)
Dim xmlOutputFormat As Boolean = False
Dim result As String = String.Empty
Try
Using client = New System.Net.Http.HttpClient()
client.BaseAddress = New Uri(serverUrl)
client.DefaultRequestHeaders.Accept.Clear()
If xmlOutputFormat Then
client.DefaultRequestHeaders.Accept.Add(New System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/xml"))
Else
client.DefaultRequestHeaders.Accept.Add(New System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"))
End If
Dim response As System.Net.Http.HttpResponseMessage = Await client.GetAsync(requestUrl)
response.EnsureSuccessStatusCode()
result = response.Content.ReadAsStringAsync().Result
Return result
End Using
Catch ex As System.Net.Http.HttpRequestException
'LogStatus(ex.Message.ToString)
Throw
End Try
Return result
End Function
JavaScript函数
function search_project_number(projectNumber) {
$.ajax({
type: "POST",
url: "/biosketch.asmx/ADMIN_SearchGrantsJSON_projectNumber_Async",
data: JSON.stringify({
projectNumber: projectNumber
}),
beforeSend: function () { },
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
build_table(data.d);
},
error: function (xhr, ajaxOptions, thrownError) {
}
});
}
我需要使用JavaScript函数来调用它,以便它可以解析JSON对象。当我使用Javascipt调用它时,我什么也没得到。没有错误,也没有成功。当我使用桌面应用程序调用它时,我可以看到JSON对象,但仍然无法解析它。这是我第一次使用HttpClient,也是第一次使用Async进行网络服务调用。我可能在这两个方面都陷入了困境,所以我需要一点帮助。我见过Promise一词-这是我所缺少的吗?感谢您的帮助