【问题标题】:Sending Data containing HTML Page to webMethod using C#使用 C# 将包含 HTML 页面的数据发送到 webMethod
【发布时间】:2015-08-18 14:29:06
【问题描述】:

我需要将 html 文件保存在我项目的文件夹之一中。为此 使用 jquery 我在一个变量中获得了页面的完整 html ( var content=$("#pageContent").html()) 。我需要使用 ajax 调用将此 html 内容发送到 Web 方法以保存在某个位置。为此,我正在使用

$.ajax({ url:'getpage.asmx/getpage', type:'POST', data:{data:content}, 成功:{function(res){}},错误:{function(er){}} });

执行上述方法时,我遇到了内部服务器错误,并且我不确定我们是否可以将具有 html 标记的字符串发送到 Web 方法。 需要一些方法来完成

【问题讨论】:

    标签: ajax webmethod


    【解决方案1】:

    我看到你的问题:

    执行上述方法我得到内部服务器错误

    我不使用 C#,但这意味着当您在 C# 中收到帖子时遇到问题。 可能是因为语法错误或类似的原因。

    当我在 PHP 中对 API 执行 AJAX 请求时,我曾经遇到过这个错误,因为我忘记了“;”例如,在 var 之后。

    您可以查看您的 C# 脚本并尝试找出问题所在。 如果您没有语法错误或其他问题, 尝试发送一个没有 html 的字符串,看看你的脚本是否有效。

    如果是,请尝试对您的内容进行编码:

    $.ajax({
        url:'getpage.asmx/getpage',
        //encode your content
        type:'POST', data:{ data: encodeURIComponent(content) },
        //text || json to set what type you will get from your C# script
        datatype: 'text',
        //callback if success res will be a text in this case
        success: function(res) {
            console.log(res);
        },
        //callback if error er will be a text in this case
        error: function(er) {
            console.log(er);
        }
    });
    

    我也误删了success:{function(res){}},,没必要

    如果您的脚本可以使用它,您可以像这样存储 html 内容, 当您需要它时,您可以在追加之前在您的 javascript 中 decodeURIComponent();

    文档:

    encodeURIComponent();

    http://www.w3schools.com/jsref/jsref_encodeURIComponent.asp

    decodeURIComponent();

    http://www.w3schools.com/jsref/jsref_decodeuricomponent.asp

    【讨论】:

    • 我试图通过用某种特殊字符替换所有 html 标记并将其发送到服务器端来解决这个问题。而在服务器端再次将这些特殊字符重新更改为 html 标签并且它工作正常。它的 html 标签正在创建将其转移到服务器端的问题。最后编写服务器代码保存在项目文件中。
    猜你喜欢
    • 1970-01-01
    • 2018-03-31
    • 1970-01-01
    • 2014-05-13
    • 2011-09-18
    • 2021-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-19
    相关资源
    最近更新 更多