Ho cercato 100 di collegamenti per le ultime 3 ore, ad esempio aggiungendo scriptfactory a webconfig , 3 errori, impostazione del tipo di contenuto ecc.Servizio Web Asmx restituendo xml invece di json, cercando di rimuovere <string xmlns = "http://tempuri.org/"> dall'output del servizio
Non sono in grado di capire cosa sia effettivamente l'errore.
Ambiente: servizio in esecuzione su .NET 4.0 Web in esecuzione su .NET 4.0
Requisiti: ho bisogno di impegnare un jqGrid con servizio web ASMX che mi restituisce un JSON come una stringa. Il file del servizio Web contiene il seguente codice.
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[ScriptService]
public class SampleService : System.Web.Services.WebService
{
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string GetJsonServerProcess()
{
int memory = 1;
string json = string.Empty;
var obj = (System.Diagnostics.Process.GetProcesses().Where(r => r.WorkingSet64 > memory).Select(p => new { p.ProcessName, p.WorkingSet64 }).ToArray());
json = Lib.ToJSON(obj);
return json;
}
}
Javascript è la seguente
<script type="text/javascript">
$(document).ready(function() {
jQuery("#jqgajax").jqGrid({
ajaxGridOptions: { type: "POST", contentType: 'application/json; charset=utf-8' },
url:'http://localhost:1092/SampleService.asmx/GetJsonServerProcess',
datatype: "json",
data: "{}",
colNames: ['ProcessName', 'WorkingSet64'],
colModel: [
{ name: 'ProcessName', index: 'ProcessName', width: 55 },
{ name: 'WorkingSet64', index: 'WorkingSet64', width: 90 }
],
rowNum: 10,
width: 700,
rowList: [10, 20, 30],
sortname: 'invdate',
viewrecords: true,
sortorder: "desc",
caption: "New API Example"
});
});
</script>
HTML è il seguente
<table id="jqgajax">
</table>
<div id="jqgajax">
</div>
uscita Web Service quando si fa clic sul pulsante Invoke
<string xmlns="http://tempuri.org/">
[{"ProcessName":"Dropbox","WorkingSet64":22736896},
{"ProcessName":"fdhost","WorkingSet64":1941504},
{"ProcessName":"IntelliTrace","WorkingSet64":39276544}
]
</string>
Si prega di suggerire cosa c'è che mi manca. I tag <string xmlns="http://tempuri.org/">
mi irritano. Suppongo che questi tag non consentano alla mia rete di legarsi.
UPDATE:
servizio ASMX ora sembra come di seguito.
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[ScriptService]
public class SampleService : System.Web.Services.WebService
{
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public List<demo> GetJsonServerProcess()
{
List<demo> test = new List<demo>();
for(int i=1;i<=10;i++)
test.Add(new demo { ProcessName = string.Format("Sample {0}",i), WorkingSet64 = i });
var re = test;
return re;
}
}
public class demo
{
public string ProcessName { get; set; }
public int WorkingSet64 { get; set; }
}
Possibile duplicato: http: //stackoverflow.com/questions/11088294/asp-net-asmx-web-service-returning-xml-instead-of-json –
Le domande sono simili ma le soluzioni sono diverse. Nella soluzione collegata, la correzione consisteva nel modificare il file web.config, in questa soluzione la correzione è modificare l'intestazione Content-Type. Tuttavia, potrebbero non essere soluzioni reciprocamente esclusive. – akousmata