简体   繁体   English

如何解决错误:无法解析从服务器收到的消息

[英]How to fix error: The message received from the server could not be parsed

We have a Sharepoint solution that uses AJAX. 我们有一个使用AJAX的Sharepoint解决方案。 The button that triggers this is inside an update panel. 触发此操作的按钮在更新面板中。

One of the things that we do is generate a MS Word document, that is then opened on the client so that it can be printed. 我们要做的一件事情是生成一个MS Word文档,然后在客户端上打开它以便可以打印它。

The code that sends the document to the client looks like this: 将文档发送到客户端的代码如下所示:

    void OpenFileInWord(byte[] data)
    {
        Response.Clear();
        Response.AddHeader("Content-Type", "application/msword");
        Response.BinaryWrite(data);
        Response.Flush();
        Response.End();
    }

The error that we are getting is: 我们得到的错误是:

Message: Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed. Common causes for this error are when the response is modified by calls to Response.Write(), response filters, HttpModules, or server trace is enabled. Details: Error parsing near '<?mso-application pr'.

We could save the document in Sharepoint first, and then open it from Sharepoint, but we would prefer not to do this. 我们可以先将文档保存在Sharepoint中,然后再从Sharepoint中打开它,但是我们不希望这样做。

如果您在updatepanel中有按钮,则可能会导致这种情况,如果不想移动它,只需在updatepanel上为按钮添加一个触发器,即回发触发器。

The action that causes this code to execute MUST be a postback event, and not an AJAX call. 导致该代码执行的动作必须是回发事件,而不是AJAX调用。

This is due to the nature of the way AJAX requests are processed. 这是由于处理AJAX请求的方式的性质所致。

Keep your button outside the update panel. 将按钮保持在更新面板之外。 Then it works fine. 然后工作正常。

尝试在页面加载中添加脚本管理器,如下所示:

((ScriptManager)Master.FindControl("ScriptManager1")).RegisterPostBackControl(btnExport);

For me the problem was duplicate control ID's in template columns of a grid view. 对我来说,问题是在网格视图的模板列中有重复的控件ID。 Once I renamed the controls to be unique grid-wide, the problem disappeared! 一旦将控件重命名为在整个网格范围内都是唯一的,问题就消失了!

On your button click, redirect to another page that can stream any files that you might want to do this type of thing with. 在您的按钮上单击,重定向到另一个页面,该页面可以流式传输您可能要使用这种类型的文件的任何文件。 We use a document.aspx page in many of our sites and then pass a document id via querystring and stream the file from there. 我们在许多站点中使用document.aspx页面,然后通过querystring传递文档ID并从那里流式传输文件。

In your example, you're basically trying to change the headers for a page that's already been displayed which isn't allowed 在您的示例中,您基本上是在尝试更改已显示页面的页眉,该页面不允许

I had an asp:Table control inside an asp:UpdatePanel control. 我在asp:UpdatePanel控件中有一个asp:Table控件。 The table had some static rows and some rows were added during a Postback event. 该表具有一些静态行,并且在回发事件期间添加了一些行。

This error occurred because rows and columns of the table did not have static IDs. 发生此错误的原因是表的行和列没有静态ID。 So IDs changed at each postback and this cause problems with restoring ViewState for the table. 因此,每次回发时ID都会更改,这会导致恢复表的ViewState问题。

To stop this error I disabled ViewState for the table: EnableViewState="false" 要停止此错误,我为表禁用了ViewState:EnableViewState =“ false”

<asp:Table ID="PageContentTable" runat="server" ... EnableViewState="false">

Set your button to cause full postback like this: 设置按钮以引起完整的回发,如下所示:

 <Triggers>
<asp:PostBackTrigger ControlID="PrintButton" />
</Triggers>

I've struggled with this for a few hours. 我为此苦了几个小时。 Doing an ajax request within my WebForms Code-Behind file wasn't working & using an UpdatePanel resulted in that annoying error and I wasn't able to find a solution. 在我的WebForms Code-Behind文件中执行ajax请求无法正常工作,并且使用UpdatePanel导致该烦人的错误,而我找不到解决方案。 Finally I found a solution that works! 终于,我找到了可行的解决方案!

  • Somewhere in your project create an .ashx (Generic Handler) file. 在项目中的某个位置创建一个.ashx (通用处理程序)文件。 I created mine within a top level services folder so: Services\\EventsHandler.ashx . 我在顶级服务文件夹中创建了我的文件夹,即: Services\\EventsHandler.ashx When you do this you should end up with both an .ashx & .ashx.cs file. 执行此操作时,您应该最终同时拥有.ashx.ashx.cs文件。
  • Inside the ashx.cs file you'll find a ProcessRequest method where you can put all of your code. ashx.cs文件中,您将找到一个ProcessRequest方法,您可以在其中放置所有代码。
  • You can call this method doing something similar to the following in your javascript file: 您可以调用此方法,并在javascript文件中执行以下操作:

     $('#MyButton').click(function () { var zipCode = $('#FBZipBox').val(); $.getJSON('/Services/EventsHandler.ashx?zip=' + zipCode, function (data) { var items = []; $.each(data.Results, function (key, item) { items.push('<span>item.Date</span>'); }); }).complete(function () { // More Logic }); return false;}); 
  • Within your ashx.cs file you can access the query parameters using: ashx.cs文件中,您可以使用ashx.cs命令访问查询参数:

    var category = context.Request.QueryString["zipCode"]; var category = context.Request.QueryString [“ zipCode”];

  • You can then return the result using: 然后,您可以使用以下方法返回结果:

    context.Response.Write(yourResponseString); context.Response.Write(yourResponseString);

我删除了按钮周围的更新面板,它工作正常。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Server.Transfer进行重定向期间出错,无法解析从服务器收到的消息。 - Error during Redirection using Server.Transfer 'The message received from the server could not be parsed.' ASP.NET-无法解析从服务器收到的消息 - ASP.NET - The message received from the server could not be parsed 下载Excel文件onclick,“无法解析从服务器收到的消息” - Download excel file onclick, “The message received from the server could not be parsed” Sys.WebForms.PageRequestManagerParserErrorException:无法解析从服务器收到的消息 - Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed 显示pdf时出错:Sys.WebForms.PageRequestManagerParserErrorException:无法解析从服务器收到的消息 - Error when displaying pdf: Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed 在ajax请求上,我们收到此错误:Sys.WebForms.PageRequestManagerParserErrorException:无法解析从服务器收到的消息 - On ajax request we get this error:Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed 如何解决“收到的消息意外或格式错误” - How to fix 'The the message received was unexpected or badly formatted' 收到:无效的 URI:无法解析主机名 - Received : Invalid URI : The hostname could not be parsed 如何修复Twitterizer的“未经授权”错误消息? - How to fix “Unauthorized” error message from Twitterizer? 服务器中收到的消息不正确 - Inaccurate received message in the server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM