【问题标题】:ASP.NET JavaScript - Invalid JSON primitiveASP.NET JavaScript - 无效的 JSON 原语
【发布时间】:2020-07-31 14:46:50
【问题描述】:

我正在尝试将“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"}

【问题讨论】:

  • 您好,我需要更多信息来帮助您。为什么不将 ASP.Net Web Api 与 Angular 或 React 或 Vuejs 一起使用?您声明了变量“区域”这是什么内容?您可以在 Github 中展示您的代码示例吗?很抱歉没有为您提供更多帮助。
  • regions 具有 html 元素的 id 及其内容。这正是我正在做的事情:getcontenttools.com/getting-started#prepare-html

标签: javascript c# asp.net json


【解决方案1】:

所以我想报告一下我是如何解决这个问题的。

显然我发现了一个困难的方法,表单数据很难阅读。我做了什么:

  1. 创建适当的 Web 服务控制器以接受 JToken 响应。
  2. 解析键值对中的响应。
  3. 根据需要将其添加到 Session 对象和数据库中。

谢谢!!

【讨论】:

    【解决方案2】:

    仅仅因为你从你的 api 端点接受一个字符串作为参数,就把它作为一个字符串发送。无需附加到 FormData。这会将您的有效负载作为对象发送。

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

    【讨论】:

    • 谢谢!我如何发送 é 接受一个对象?如果我将我的方法更改为这样,它仍然不起作用: public static object getJSONHTMLResponse(object name) { return "Success"; }
    • 1.创建一个名为:[Serializable] public class Region { public string paraNo {get; set; } public string paraText {get; set;} } 的类 2.将方法更改为:public static string methodName(Region region){ return "success"; } 3.根据区域类属性创建ajax发送的对象。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-29
    • 1970-01-01
    • 1970-01-01
    • 2019-08-24
    • 2014-04-17
    相关资源
    最近更新 更多