【问题标题】:403 error when posting HTML code as a JSON value将 HTML 代码作为 JSON 值发布时出现 403 错误
【发布时间】:2017-09-19 15:33:53
【问题描述】:

我有一个 ASP.NET WebAPI 端点,它接受 JSON 以推送到另一个第三方 API。我在将 HTML 发布为 JSON 值时遇到问题,如下所示:

var attr1 = {};
attr1.attributeid = 1234;
attr1.attributevalue = '<div>test</div>';
output.attributes.push(attr1);

结果是 HTTP 403 错误。

但是,当将普通字符串作为 JSON 推送时,它可以工作,HTTP 200。

var attr1 = {};
attr1.attributeid = 1234;
attr1.attributevalue = 'test';
output.attributes.push(attr1);

WebAPI 后端位于 .NET 4.6.2 上,每个请求都需要 OAuth 2.0 Bearer 令牌。其他需要注意的是,CORS 已启用,API 控制器操作如下所示:

        public async Task<IHttpActionResult> Post([FromBody] Email email)
        {
            var emailResult = new Email();
            try
            {
                using (var svc = new EmailService())
                {
                    emailResult = await svc.SendTransactionalEmail(email);
                }

                if (emailResult != null)
                {
                    if (emailResult.Success)
                    {
                        return Ok("Transactional email successfully sent!");
                    }
                    else
                    {
                        return BadRequest(emailResult.Result);
                    }
                }
                else
                {
                    return BadRequest();
                }
            }
            catch (Exception e)
            {
                return InternalServerError(e);
            }
        }

【问题讨论】:

    标签: asp.net json asp.net-web-api netsuite http-status-code-403


    【解决方案1】:

    尝试转义 attr1.attributevalue 中的正斜杠,如下所示 '&lt;div&gt;test&lt;\/div&gt;'

    这可能会有所帮助! :)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-12-27
      • 2017-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-06
      相关资源
      最近更新 更多