javascript - ASP.NET JavaScript - 无效的 JSON 原语

标签 javascript c# asp.net json

我正在尝试将“contentTools”与 ASP.NET C# 网站一起使用。我正在尝试将修改后的 json 发送到服务器端以存储在数据库中。

我已经通过外部网络验证了 JSON,但这是我的代码。出于测试目的,我尝试使用从 ASP.NET 方法发回的字符串来调用警报。

window.onload = function () {
        //ShowCurrentTime();
        var FIXTURE_TOOLS, IMAGE_FIXTURE_TOOLS, LINK_FIXTURE_TOOLS, editor;
        ContentTools.IMAGE_UPLOADER = ImageUploader.createImageUploader;
        ContentTools.StylePalette.add([new ContentTools.Style('By-line', 'article__by-line', ['p']), new ContentTools.Style('Caption', 'article__caption', ['p']), new ContentTools.Style('Example', 'example', ['pre']), new ContentTools.Style('Example + Good', 'example--good', ['pre']), new ContentTools.Style('Example + Bad', 'example--bad', ['pre'])]);
        editor = ContentTools.EditorApp.get();
        editor.init('[data-editable], [data-fixture]', 'data-name');
        editor.addEventListener('saved', function (ev) {
            var name, payload, regions, xhr;

            // Check that something changed
            regions = ev.detail().regions;
            if (Object.keys(regions).length == 0) {
                return;
            }

            // Set the editor as busy while we save our changes
            this.busy(true);

            // Collect the contents of each region into a FormData instance
            payload = new FormData();
            payload.append('regions', JSON.stringify(regions));

            xhr = new XMLHttpRequest();
            xhr.open('POST', 'Default.aspx/getJSONHTMLResponse');
            xhr.setRequestHeader("Content-Type", "application/json");
            xhr.onreadystatechange = function () {
                if (xhr.readyState == 4 && xhr.status == 200) {
                    alert(xhr.responseText);
                }
            };            
            xhr.send(payload);


        });

Here is my server-side:

    [System.Web.Services.WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public static string getJSONHTMLResponse(string name)
    {
        return "Success";
    }

这是我在 chrome 开发者工具中看到的请求负载:

------WebKitFormBoundaryilrxnMxm7ANdiYMp
Content-Disposition: form-data; name="regions"

{"para-1":"<p>\n testestest\n</p>","para-2":"<p>\n testestestestest.\n</p>"}
------WebKitFormBoundaryilrxnMxm7ANdiYMp--

错误:

{"Message":"Invalid JSON primitive: ------WebKitFormBoundaryilrxnMxm7ANdiYMp.","StackTrace":"   at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializePrimitiveObject()\r\n   at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 depth)\r\n   at System.Web.Script.Serialization.JavaScriptObjectDeserializer.BasicDeserialize(String input, Int32 depthLimit, JavaScriptSerializer serializer)\r\n   at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer serializer, String input, Type type, Int32 depthLimit)\r\n   at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize[T](String input)\r\n   at System.Web.Script.Services.RestHandler.GetRawParamsFromPostRequest(HttpContext context, JavaScriptSerializer serializer)\r\n   at System.Web.Script.Services.RestHandler.GetRawParams(WebServiceMethodData methodData, HttpContext context)\r\n   at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.ArgumentException"}

最佳答案

因为您从 api 端点接受字符串作为参数,所以将其作为字符串发送。无需附加到 FormData 中。这会将您的有效负载作为对象发送。

xhr.send(JSON.stringify(regions));

关于javascript - ASP.NET JavaScript - 无效的 JSON 原语,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61282824/

相关文章:

javascript - 基于对象属性对数组进行排序 - Javascript

javascript - 从零开始学习YUI

c# - 在另一个程序集中重用一个对象..需要帮助知道在哪里看/要学习什么以获得答案

c# - Telerik RadCaptcha/我们如何从代码隐藏/不使用验证组验证 RadCaptcha

c# - 使用 C# 代码 (ASP.net) 选择 CSS 样式

c# - 解析 HTML 片段

javascript - 根据匹配的 ID 合并两个数组中的项目

javascript - jQuery 电话号码屏蔽与正则表达式不起作用

c# - 引用类型自动属性的自动实例化

c# - 无法授权 Azure LogAnalytics 工作区