ext js Form.Submit总是返回失败

问题描述 投票:0回答:1

大家早上好,

我正在尝试一个非常基本的表单。提交,由于某种原因它总是失败。 还使用 .NET 中的本地 Web 服务。

我一定错过了一些非常基本的东西......或者可能是数据返回方式的一些东西。

我附上了几张图片来展示我的尝试:

image1 - 表单.提交

image2 - 服务.cs

image3 - 我如何从本地 .NET Web 服务返回结果

对图像表示歉意...由于某种原因,剪切和粘贴代码不起作用。

谢谢! 斯蒂芬

这是Web服务中调试器的图片 第一行是 var 变量 第二行 (x2) 使用 JsonConvert.SerializeObject 转换为 json

最后是浏览器返回时调试器的图片

我也一直在尝试了解 CORS...所以我将以下内容添加到我的 web.config 中,但这没有帮助

  <system.webServer>
    <httpProtocol>
      <customHeaders>
        <add name="Access-Control-Allow-Origin" value="*"/>
        <add name="Access-Control-Allow-Headers" value="Origin, X-Requested-With, Content-Type, Accept" />
        <add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
      </customHeaders>
    </httpProtocol>
  </system.webServer>

这是 Firefox 调试器

我调整了 web.config 以允许选项,但仍然出现错误 状态代码 405 不允许使用方法

  <system.webServer>
    <httpProtocol>
      <customHeaders>
        <add name="Access-Control-Allow-Origin" value="*"/>
        <add name="Access-Control-Allow-Headers" value="Content-Type" />
        <add name="Access-Control-Allow-Methods" value="OPTIONS, TRACE, GET, HEAD, POST, PUT" />
        <!--<add name="Access-Control-Allow-Headers" value="Origin, X-Requested-With, Content-Type, Accept" />
        <add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />-->
      </customHeaders>
    </httpProtocol>
  </system.webServer>

好的...我做了一些更改...我从 web.config 中删除了这些行,并添加了一个 Global.asax.cs 页面,其中包含以下内容:

protected void Application_BeginRequest(object sender, EventArgs e)
{
    HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
    if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
    {
        HttpContext.Current.Response.AddHeader("Cache-Control", "no-cache");
        HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST");
        HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept, Authorization, X-Requested-With");
        HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");
        HttpContext.Current.Response.End();
    }
}

现在看似成功了,但还是失败了。我在这里有什么事情吗?是我的json格式吗?

结果

c# json extjs
1个回答
2
投票

您的响应包含反斜杠,这不是有效的 JSON。它必须是

{"success":true}
,不带反斜杠。

这应该是由于双重序列化造成的。你调试到哪里就OK了。但您应该在其他地方再次序列化它。确保避免双重序列化。

© www.soinside.com 2019 - 2024. All rights reserved.