Drodzy przyszłych czytelników: Aktualnie nie jest akceptowane odpowiedź właściwą drogę. To wiąże cię z używaniem JavaScriptSerializer
i tracisz możliwość żądania xml (lub jakiegokolwiek formatu serializacji, który może pojawić się w przyszłości). "Właściwa droga" obejmuje również mniej kodu!
Jeśli ozdobić swoją klasę usług z atrybutem [ScriptService]
- co trzeba - wtedy ASP.NET 3.5+ powinien automatycznie serialise odpowiedzi na JSON warunkiem swoich żądań Ajax połączeń JSON. Sugestie serializowania do JSON ręcznie są po prostu błędne, chyba że chcesz użyć innego serializera, takiego jak Newtonsoft.
że jesteś widząc XML sugeruje jedną z następujących czynności:
- Nie żądają JSON w wywołaniu Ajax - proszę zobaczyć pracę przykładowy kod poniżej
- ewentualnie niektóre wpisy web.config brakuje, zgodnie na odpowiedź: here (wyłączenie odpowiedzialności: większość z nich nie jest dostępna w sieci internetowej o nazwie .config; tylko zacząć grać z nimi, jeżeli nic innego nie działa)
Oto prosty przykład pracy z JSON włączona ASMX serwis internetowy:
<%@ WebService Language="C#" Class="WebService" %>
using System;
using System.Collections.Generic;
using System.Web.Services;
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService {
[WebMethod]
public MyClass Example()
{
return new MyClass();
}
public class MyClass
{
public string Message { get { return "Hi"; } }
public int Number { get { return 123; } }
public List<string> List { get { return new List<string> { "Item1", "Item2", "Item3" }; } }
}
}
JavaScript, żeby poprosić go i przetwarza odpowiedź (my po prostu pop-up alert JS z komunikatem z MyClass.Message): Prośba
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Test</title>
<script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.6.4.js" type="text/javascript"></script>
</head>
<body>
<script type="text/javascript">
$.ajax({
type: "POST",
url: "WebService.asmx/Example",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: "{ }",
error: function (XMLHttpRequest, textStatus, errorThrown) { alert(langError + " " + textStatus); },
success: function (msg) {
alert(msg.d.Message);
}
});
</script>
</body>
</html>
http:
POST http://HOST.com/WebService.asmx/Example HTTP/1.1
Accept: application/json, text/javascript, */*; q=0.01
Content-Type: application/json; charset=utf-8
X-Requested-With: XMLHttpRequest
Referer: http://HOST.com/Test.aspx
Accept-Language: en-GB,en;q=0.5
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)
Connection: Keep-Alive
Content-Length: 3
Host: HOST.com
{ }
Odpowiedź
HTTP:
HTTP/1.1 200 OK
Cache-Control: private, max-age=0
Content-Type: application/json; charset=utf-8
Server: Microsoft-IIS/8.0
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Tue, 20 Feb 2018 08:36:12 GMT
Content-Length: 98
{"d":{"__type":"WebService+MyClass","Message":"Hi","Number":123,"List":["Item1","Item2","Item3"]}}
Wynik:
wyświetlany jest komunikat "Hi" w okienko JS.
Możliwy duplikat [Pobierz dane JSON z jQuery z usługi .NET: zdezorientowany z ustawieniem ajaxowego] (http://stackoverflow.com/questions/5690882/get-json-data-with-jquery-from-a- net-service-confused-with-ajax-setup) – GSerg
Możliwy duplikat [usługi sieciowej Asmx, jak zwrócić JSON, a nie XML?] (https://stackoverflow.com/questions/14950578/asmx-web-service-how- to-return-json-and-not-xml) –