Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用jQuery.ajax()和.NET web服务提交HTML表_C#_Asp.net_Web Services_Jquery - Fatal编程技术网

C# 使用jQuery.ajax()和.NET web服务提交HTML表

C# 使用jQuery.ajax()和.NET web服务提交HTML表,c#,asp.net,web-services,jquery,C#,Asp.net,Web Services,Jquery,我对jQuery和JavaScript非常陌生,但我想向我编写的.NETWeb服务提交一封电子邮件。我已经用数据中的一些简单值测试了web服务,但我希望在完成客户端和服务器端验证后,可以提交一个完整的表,填充服务器端 这项工作: data: '{ strFrom: "abcd@abc.edu", to: "abcd@abc.edu", cc: "abcd@abc.edu", subject: "test subject", body: "test body" }' 但是,正如我所说,我已经用对

我对jQuery和JavaScript非常陌生,但我想向我编写的.NETWeb服务提交一封电子邮件。我已经用数据中的一些简单值测试了web服务,但我希望在完成客户端和服务器端验证后,可以提交一个完整的表,填充服务器端

这项工作:

data: '{ strFrom: "abcd@abc.edu", to: "abcd@abc.edu", cc: "abcd@abc.edu", subject: "test subject", body: "test body" }'
但是,正如我所说,我已经用对表单的响应填充了这个表,当我执行var body=$divSuccessDetails.html;通过它,而不是测试体,我得到一个无效的对象响应。我已经测试了我选择的表格的结果,结果如下:

<table id="ctl00_ContentPlaceHolder1_tblSuccessDetails" class="successDetails" style="width:80%;">
    <tbody><tr>
        <td class="successHeading" colspan="2">
    Reporting Category
    </td>
    </tr>
    <tr>
        <td>
        <span id="ctl00_ContentPlaceHolder1_lblBilling">Billing Provider: <b></b></span>
    </td>
        <td rowspan="1">
        <span id="ctl00_ContentPlaceHolder1_lblCategory">Reporting Category: </span></td>
    </tr>
    <tr>
我收到的错误消息如下:

"Invalid object passed in, ':' or '}' expected. (176): { strFrom: "abcd@abc.edu", to: "abcd@abc.edu", cc: "abcd@abc.edu", subject: "IDX Provider Form by IDX Provider Form by abcd", body: " <table id="ctl00_ContentPlaceHolder1_tblSuccessDetails" class="successDetails" style="width:80%;"> <tbody><tr> <td class="successHeading" colspan="2">

我只是希望把它作为一个字符串传递,因为这就是我收到的。。我已经将大量HTML放入字符串中,我以前在emailobject中使用过这些字符串,但这是我第一次尝试通过AJAX将其传递。

如果您尝试将整个HTML表(包含HTML元素)传递给web服务,您将遇到HTML字符问题,例如引号、双引号等。。。。您可以转义字符,但我认为您最好在HTML表中找到要发送的特定数据值,并将它们分配给数据对象,因为这只会导致发送数据而不是标记和数据。如果将整个HTML表传递给服务器,则必须解析HTML文档以提取值

作为另一种解决方案,您可以使用jQuery创建一个页面,在该页面上可以对其进行处理

$.post("FormProcessor.aspx", $("#testform").serialize());
[更新]

如果只想将表的HTML标记发送到服务器,则需要对结果进行HTML编码,并将值作为字符串值传递给服务器

    jQuery.fn.EncHTML = function () {
        return String(this.html())
            .replace(/&/g, '&amp;')
            .replace(/"/g, '&quot;')
            .replace(/'/g, '&#39;')
            .replace(/</g, '&lt;')
            .replace(/>/g, '&gt;');
    };
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
public class TableManager : WebService
{
    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public void Process(string tableContents)
    {
        var table = Server.HtmlDecode(tableContents);
        // Do Something (Email, Parse, etc...).
    }
}
这将调用TableManager.asmx服务的Process方法。它需要传递一个名为tableContentns的字符串类型的变量。它的返回类型为VOID,因此不会将任何内容发送回客户机/页面,但可以将其更改为string,以便您可以发送一条消息,说明表/数据已被处理

$.post("FormProcessor.aspx", $("#testform").serialize());
注意:我还使用该函数对发送到服务器的对象进行正确的JSON编码

    jQuery.fn.EncHTML = function () {
        return String(this.html())
            .replace(/&/g, '&amp;')
            .replace(/"/g, '&quot;')
            .replace(/'/g, '&#39;')
            .replace(/</g, '&lt;')
            .replace(/>/g, '&gt;');
    };
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
public class TableManager : WebService
{
    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public void Process(string tableContents)
    {
        var table = Server.HtmlDecode(tableContents);
        // Do Something (Email, Parse, etc...).
    }
}
这是服务器上的web方法

    jQuery.fn.EncHTML = function () {
        return String(this.html())
            .replace(/&/g, '&amp;')
            .replace(/"/g, '&quot;')
            .replace(/'/g, '&#39;')
            .replace(/</g, '&lt;')
            .replace(/>/g, '&gt;');
    };
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
public class TableManager : WebService
{
    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public void Process(string tableContents)
    {
        var table = Server.HtmlDecode(tableContents);
        // Do Something (Email, Parse, etc...).
    }
}

从客户端拉回来的表将不包括表标记,因此您需要将这些标记放入自己的表中。

我在这里没有看到web服务。是否缺少[WebMethod]属性?是的,很抱歉,我没有将所有内容都设置为使其更小。明白了,所以您只需对网格进行HTML编码,并将其作为字符串参数发送到服务器。没有内置的编码功能,但我在回复中添加了一个。