我有一个简单的网络服务我对此进行了调查并得到了正确答案,代码是:
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public String GetUserId()
{
var windowsUserDomain = System.Web.HttpContext.Current.User.Identity.Name;
var windowsUser = windowsUserDomain.Split('\\')[1];
IUser user = Zamba.Core.UserBusiness.GetUserByname(windowsUser);
//this.Context.Response.ContentType = "application/json; charset=utf-8";
//JavaScriptSerializer jsonSerialiser = new JavaScriptSerializer();
//return jsonSerialiser.Serialize(jsonList);
return Newtonsoft.Json.JsonConvert.SerializeObject(user);
}
当我在Chrome中打开此WS时,我有以下代码:
This XML file does not appear to have any style information associated with it. The document tree is shown below.
<string xmlns="http://tempuri.org/">
{"Password":"","Crdate":"0001-01-01T00:00:00","Lmoddate":"2014-04-16T00:00:00","Expirationtime":0,"AddressBook":"","Expiredate":"20/07/2005","Nombres":"Dl","Apellidos":"Sri","puesto":"CLAIMS","telefono":"53","firma":"","Picture":"\\\\server2008\\Volumenes\\inDanil.jpg","ConnectionId":0,"WFLic":false,"eMail":{"Type":1,"Servidor":"","Base":"","Mail":"mauso@arc.com.ar","UserName":"mroc.com.ar","ProveedorSMTP":"smtp.gmail.com","Puerto":57,"Password":""},"Groups":[{"IsFull":false,"IsLoaded":false,"State":0,"Type":1,"Description":"","Users":[],"CreateDate":"","PictureId1":0,"Name":"* CLAIMS","ID":2145}],"PictureId1":0,"Type":0,"Description":"Marsh S.A.","State":1,"IsFull":false,"IsLoaded":false,"MailConfigLoaded":false,"Name":"sta","ID":258}
</string>
但是当我在这种模式下使用AJAX在JS中调用这个WS时:
$.ajax({
type: "GET",
async: false,
url: "Services/UsersInfo.asmx/GetUserId",
success: function (d) {
thisUserId = d.User;
}
});
d = HTML中的文档,而不是JSON响应,我在web.config中输入了这段代码:
<add name="ScriptHandlerFactory"
verb="*" path="*.asmx"
type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
resourceType="Unspecified" />
但是每次返回文档而不是JSON响应时,我都无法做到&#34; d.data&#34;或$ .JSON(d)因为d是文档。
如何配置我的WS,AJAX调用或Web配置?感谢