javascript - 当我想要一个字符串时用于响应的 Html

标签 javascript html asp.net json ajax

我有以下功能:

function getHtmlFromMarkdown(markdownFormat, requestUrl) {

    const dataValue = { "markdownFormat": markdownFormat }

    $.ajax({
        type: "POST",
        url: requestUrl,
        data: dataValue,
        contentType: "application/json: charset = utf8",
        dataType: "text",
        success: function (response) {
            alert(response);
            document.getElementById("generatedPreview").innerHTML = response;
        },
        fail: function () {
            alert('Failed')
        }
    });
}

我的服务器上有这个:

    [WebMethod]
    public static string GenerateHtmlFromMarkdown(string markdownFormat)
    {
        string htmlFormat = "Some text";

        return htmlFormat;
    }

我有响应 html 代码,而不是我想要的字符串。我究竟做错了什么? 如果我更改“dataType:json”,它甚至不会输入成功或失败函数

最佳答案

你的ajax数据类型必须是这样的json

function getHtmlFromMarkdown(markdownFormat, requestUrl) {
    var dataValue = { "markdownFormat": markdownFormat }
    $.ajax({
        type: "POST",
        url: requestUrl,
        data: JSON.stringify(dataValue),
        dataType: "json",
        success: function (response) {
            alert(response);
            document.getElementById("generatedPreview").innerHTML = response;
        },
        error: function () { alert("Failed"); }
    });
}

关于javascript - 当我想要一个字符串时用于响应的 Html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50387894/

相关文章:

asp.net - IIS 应用程序池不会在组托管服务帐户下启动

javascript - backbone.js View 更改时更新模型的正确方法

javascript - 我可以在 JavaScript 中查看哪些数据库实现模式?

c# - ASP.NET - 检查请求是否在母版页中通过身份验证

html - 如何使图像成为 'jump to' 按钮?

python - Django 错误信息 : ["' on' value must be either True or False."]

jquery - 回发后元素在 jQuery 对象中保留原始值

javascript - Node.js/Puppeteer - DOM NodeList 到 JS 对象

javascript - 在表的开头添加/附加列

html - 愚蠢的 CSS 引用问题