[英]$.ajax not calling WebService in ASP.Net
我正在嘗試通過jQuery調用網絡服務,但未顯示任何結果或錯誤。 我的代碼是:
<script type="text/javascript" language="javascript">
var empId = '<%= Session["UserName"] %>';
</script>
<script type="text/javascript">
alert(empId);
$.ajax({
type: "POST",
dataType: "json",
contentType: "application/json",
url: "ServiceGetUser.asmx/GetEmployee",
data: "{'employeeId': '" + empId + "'}",
success: function (data) {
alert("Employee name: " + data.d);
},
error: function () {
alert("Error calling the web service.");
}
});
</script>
我從session
成功打印中獲取了一個值,然后將其傳遞到Web服務,並返回打印名稱Jane Developer
,如我的Web服務代碼所示:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
namespace MentorMentee
{
/// <summary>
/// Summary description for ServiceGetUser
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class ServiceGetUser : System.Web.Services.WebService
{
[WebMethod]
public string GetEmployee(string employeeId)
{
return "Jane Developer";
}
}
}
出了什么問題希望您提出建議
謝謝
您可以將其放在doc ready block
:
$(window).load(function(){
$.ajax({
type: "POST",
dataType: "json",
contentType: "application/json",
url: "ServiceGetUser.asmx/GetEmployee",
data: {'employeeId': '<%= Session["UserName"] %>'}, //<-- try it
success: function (data) {
alert("Employee name: " + data.d);
},
error: function () {
alert("Error calling the web service.");
}
});
});
dataType作為json給出。 這意味着jquery將將從服務器接收到的響應解析為json。 但是您在服務中返回字符串。 因此解析錯誤將由jQuery引發。
嘗試附加complete(jqXHR jqXHR, String textStatus)
回調。 並查看textStatus
嘗試使用
<script type="text/javascript" language="javascript">
var empId = '<%= Session["UserName"] %>';
</script>
<script type="text/javascript">
alert(empId);
$.ajax({
type: "POST",
dataType: "json",
contentType: "application/json",
url: "ServiceGetUser.asmx/GetEmployee",
data: "{'employeeId': '" + empId + "'}",
success: function (data) {
alert("Employee name: " + data.d);
},
error: function () {
alert("Error calling the web service.");
} ,
complete: function(xhr, textStatus) {
alert(textStatus);
}
});
</script>
嘗試這個:
<script type="text/javascript" language="javascript">
var empId = '<%= Session["UserName"] %>';
</script>
<script type="text/javascript">
alert(empId);
$.ajax({
type: "POST",
dataType: "json",
contentType: "application/json; charset=utf-8",
url: "ServiceGetUser.asmx/GetEmployee",
data: '{employeeId: "' + $("#<%=employeeId.ClientID%>")[0].value + '" }',
success: function (data) {
alert("Employee name: " + data.d);
},
error: function () {
alert("Error calling the web service.");
}
});
</script>
首先嘗試像這樣的alert(JSON.stringify(data))
對您的json進行字符串化alert(JSON.stringify(data))
然后您可能會發現您的錯誤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.