我一直在尋找最近3個小時的100個鏈接,例如將scriptfactory添加到webconfig ,3個錯誤,設置內容類型等。Asmx web服務返回xml而不是json,試圖從服務輸出中刪除<string xmlns =「http://tempuri.org/」>
我無法弄清楚究竟是什麼錯誤。
環境:在.NET 4.0 Web應用程序運行在.NET 4.0
要求運行 服務: 我需要一個jqGrid的綁定與正在返回我JSON作爲字符串ASMX Web服務。 Web服務文件包含以下代碼。
[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是如下
<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是Invoke按鈕,當點擊如下
<table id="jqgajax">
</table>
<div id="jqgajax">
</div>
Web服務輸出
<string xmlns="http://tempuri.org/">
[{"ProcessName":"Dropbox","WorkingSet64":22736896},
{"ProcessName":"fdhost","WorkingSet64":1941504},
{"ProcessName":"IntelliTrace","WorkingSet64":39276544}
]
</string>
請提出我缺少的東西。 <string xmlns="http://tempuri.org/">
標籤讓我很煩惱。我假設這些標籤不讓我的網格能夠綁定。
UPDATE:
ASMX服務現在看起來如下。
[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; }
}
可能的重複:http://stackoverflow.com/questions/11088294/asp-net-asmx-web-service-returning-xml-instead-of-json –
問題是類似的,但解決方案是不同的。在鏈接解決方案中,修復程序是修改web.config文件,在此解決方案中,修復程序是修改Content-Type標頭。儘管如此,它們可能不是相互排斥的解 – akousmata