我正在嘗試使用jQuery調用Web服務。出於某種原因,結果以XML的形式返回......除了自己編寫解析器之外,還有一種更好的方式來獲得結果。如何解析或訪問從jQuery的Web服務調用返回的值?
這是返回的值:
<?xml version="1.0" encoding="utf-8"?>\r\n<string xmlns="http://tempuri.org/">"Hello World"</string>
這是HTML:
<script type="text/javascript">
var url = '<%=ResolveUrl("~/Services/ProjectDialog.asmx/HelloWorld")%>';
function callWebService() {
jQuery.ajax({
cache: false,
type: 'POST',
complete: onComplete,
data: null,
dataType: 'application/json; charset=utf-8',
error: onError,
success: onSuccess,
url: url
});
}
function onComplete(status, xmlHttpRequest) {
var stop = "";
}
function onError(xmlHttpRequest, status, error) {
var stop = "";
}
function onSuccess(data, status, xmlHttpRequest) {
var stop = "";
}
jQuery(document).ready(function() {
});
</script>
<input type="button" value="Run Web Service" onclick="callWebService();" />
這是Web服務:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Serialization;
namespace My.Services
{
/// <summary>
/// Summary description for ProjectDialog
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[System.Web.Script.Services.ScriptService]
public class ProjectDialog : System.Web.Services.WebService
{
[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
}
}
看到我的更新。如果你指定'contentType',你將得到一個有效的'json'響應。 – 2011-04-04 15:12:24