jQuery   发布时间:2022-04-19  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了jquery – 返回xml而不是json的Asmx web服务,试图从服务输出中删除大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我一直在寻找过去3个小时的100个链接,例如将scriptfactory添加到webconfig,3个错误,设置内容类型等.

我无法弄清楚究竟是什么错误.

环境:
服务在.net 4.0上运行
在.net 4.0上运行的Web应用程序

要求:
我需要将jqGrid绑定到asmx web服务,该服务将json作为字符串返回给我.
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如下

<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/”>标签让我很烦.我假设这些标签不让我的网格能够绑定.

更新:

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; }
}

解决方法

单击Invoke按钮返回XML,因为请求未指定contentType:’application / json;字符集= UTF-8′ .因此,单击“调用”按钮的实验并不能帮助您.

代码中的主要问题是您将数据转换为Web方法内的字符串.这条线

json = Lib.ToJSON(obj);

不需要.通常做的是返回对象.应该将GetJsonServerProcess更改为类似的内容

[Scriptservice]
public class Sampleservice : System.Web.services.Webservice
{
    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public List<Process> GetJsonServerProcess()
    {
        int memory = 1;
        return System.Diagnostics.process.GetProcesses()
                   .Where(r => r.WorkingSet64 > memory)
                   .SELEct(p => new { p.processName,p.WorkingSet64 })
                   .ToList();
    }
}

一个问题是等待jqGrid的认输入格式是另一个(见here).所以你要指出描述数据格式的jsonReader.在你的情况下它会是这样的

jsonReader: {
    repeatitems: false,id: "ProcessName",root: function (obj) { return obj; },page: function () { return 1; },@R_847_10586@l: function () { return 1; },records: function (obj) { return obj.length; }
}

此外,您不应在Ajax URL中使用http:// localhost:1092 /前缀,因为出于安全原因,您只能从同一站点获取数据. jqGrid中的data参数具有jQuery中的另一个含义,因此您应该删除数据:“{}”并将类型:“POST”从ajaxGridoptions移动到mtype:“POST”.结果你会有类似的东西

$(document).ready(function () {
    $("#jqgajax").jqGrid({
        mtype: "POST",ajaxGridoptions: { contentType: 'application/json; charset=utf-8' },url: '/Sampleservice.asmx/GetJsonServerProcess',postData: "{}",// remove all parameters which jqGrid send typically
        datatype: "json",colmodel: [
            { name: 'ProcessName',width: 155 },width: 190 }
        ],jsonReader: {
            repeatitems: false,records: function (obj) { return obj.length; }
        },loadonce: true,gridview: true,height: 'auto',caption: "New API Example"
    });
});

我没有测试代码,但它应该更接近你需要的.

更新:您应该通过更改jsonReader来修复代码.您可以下载工作演示here.它显示网格

我在服务器端使用了代码

using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Web.services;

namespace jqGridWebASMX
{
    [Webservice(Namespace = "http://tempuri.org/")]
    [WebserviceBinding(ConformsTo = WsiProfiles.basicProfile1_1)]
    [System.ComponentModel.ToolBoxItem(false)]
    [System.Web.Script.services.Scriptservice]
    public class Sampleservice : Webservice
    {
        [WebMethod]
        public List<Demo> GetJsonServerProcess()
        {
            const int memory = 1;
            return Process.GetProcesses()
                .Where (r => r.WorkingSet64 > memory)
                .SELEct(p => new Demo {
                    Id = p.Id,ProcessName = p.processName,WorkingSet64 = p.WorkingSet64
                })
                .ToList();
        }
    }

    public class Demo
    {
        public int id { get; set; }
        public String ProcessName { get; set; }
        public long WorkingSet64 { get; set; }
    }
}

在客户端

$("#list").jqGrid({
    mtype: "POST",// remove all parameters which jqGrid send typically
    datatype: "json",colmodel: [
        { name: 'ProcessName',width: 200 },width: 120,formatter: 'Integer',sorttype: 'int',align: 'right' }
    ],jsonReader: {
        repeatitems: false,id: "Id",root: function (obj) { return obj.d; },records: function (obj) { return obj.d.length; }
    },pager: '#pager',rownumbers: true,caption: "New API Example"
});
$("#pager_left").hide(); // hide unused part of the pager to have more space

大佬总结

以上是大佬教程为你收集整理的jquery – 返回xml而不是json的Asmx web服务,试图从服务输出中删除全部内容,希望文章能够帮你解决jquery – 返回xml而不是json的Asmx web服务,试图从服务输出中删除所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。