我試圖通過使用jQuery的GET方法調用Web服務函數,但遇到問題。這是一個Web服務代碼:通過使用jQuery的GET方法調用ASP.NET Web服務函數
[WebService(Namespace = "http://something.com/samples")]
[ScriptService]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class TestWebService : System.Web.Services.WebService {
[WebMethod]
public string Test2()
{
string result = null;
try
{
result = "{'result':'success', 'datetime':'" + DateTime.Now.ToString() + "'";
}
catch (Exception ex)
{
result = "Something wrong happened";
}
return result;
}
}
這就是我所說的功能方式:
$.ajax({ type: "GET",
url: "http://localhost/testwebsite/TestWebService.asmx/Test2",
data: "{}",
contentType: "application/json",
dataType: "json",
error: function (xhr, status, error) {
alert(xhr.responseText);
},
success: function (msg) {
alert('Call was successful!');
}
});
方法調用成功,但結果字符串被通過XML標籤覆蓋,這樣的:
<string>
{'result':'success', 'datetime':'4/26/2010 12:11:18 PM'
</string>
而我得到一個錯誤,因爲這(錯誤處理程序被稱爲)。有人知道可以做些什麼嗎?
你的'}'在返回的JSON字符串的末尾? – 2010-04-27 13:08:21
你的意思是由XML標籤覆蓋的json字符串?當我從瀏覽器窗口複製它時,我認爲我做錯了什麼,當然它應該在那裏。 – 2010-04-27 16:00:55
也許,但我沒有在Web服務代碼中看到它。 – 2010-04-27 17:56:22