C# 如何修复错误:无法解析从服务器收到的消息

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1554728/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 18:48:00  来源:igfitidea点击:

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

c#ajaxsharepoint

提问by Shiraz Bhaiji

We have a Sharepoint solution that uses AJAX. The button that triggers this is inside an update panel.

我们有一个使用 AJAX 的 Sharepoint 解决方案。触发此操作的按钮位于更新面板内。

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'.

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 中打开它,但我们不希望这样做。

采纳答案by Mitchel Sellers

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 请求的方式的性质造成的。

回答by Anthony Shaw

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 页面,然后通过查询字符串传递文档 ID 并从那里传输文件。

In your example, you're basically trying to change the headers for a page that's already been displayed which isn't allowed

在您的示例中,您基本上是在尝试更改已显示的页面的标题,这是不允许的

回答by Madhu

Keep your button outside the update panel. Then it works fine.

将您的按钮保持在更新面板之外。然后它工作正常。

回答by MauricioMCP

If you have the button inside updatepanel this maybe causing this, if don′t want to move it, just add a trigger for the button on the updatepanel, a postback trigger.

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

回答by Daya

I removed the update panel around the button and it worked fine.

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

回答by Dave de Jong

For me the problem was duplicate control ID's in template columns of a grid view. Once I renamed the controls to be unique grid-wide, the problem disappeared!

对我来说,问题是网格视图的模板列中的控件 ID 重复。一旦我将控件重命名为唯一的网格范围,问题就消失了!

回答by Manick

Try adding script manager in your page load, like this:

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

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

回答by Eduard Silantiev

I had an asp:Table control inside an asp:UpdatePanel control. The table had some static rows and some rows were added during a Postback event.

我在 asp:UpdatePanel 控件中有一个 asp:Table 控件。该表有一些静态行,并且在回发事件期间添加了一些行。

This error occurred because rows and columns of the table did not have static IDs. So IDs changed at each postback and this cause problems with restoring ViewState for the table.

发生此错误是因为表的行和列没有静态 ID。因此,每次回发时 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">

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

回答by Masoumeh Karvar

Set your button to cause full postback like this:

设置您的按钮以导致完整的回发,如下所示:

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

回答by Colin

I've struggled with this for a few hours. Doing an ajax request within my WebForms Code-Behindfile wasn't working & using an UpdatePanelresulted in that annoying error and I wasn't able to find a solution. Finally I found a solution that works!

我已经为此挣扎了几个小时。在我的 WebFormsCode-Behind文件中执行 ajax 请求不起作用并使用UpdatePanel导致那个烦人的错误,我无法找到解决方案。最后我找到了一个有效的解决方案!

  • Somewhere in your project create an .ashx(Generic Handler) file. I created mine within a top level services folder so: Services\EventsHandler.ashx. When you do this you should end up with both an .ashx& .ashx.csfile.
  • Inside the ashx.csfile you'll find a ProcessRequestmethod where you can put all of your code.
  • You can call this method doing something similar to the following in your javascript file:

    $('#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.csfile you can access the query parameters using:

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

  • You can then return the result using:

    context.Response.Write(yourResponseString);

  • 在您的项目中的某处创建一个.ashx(通用处理程序)文件。我在顶级服务文件夹中创建了我的:Services\EventsHandler.ashx. 当你这样做时,你应该最终得到一个.ashx&.ashx.cs文件。
  • 在该ashx.cs文件中,您将找到一个ProcessRequest方法,您可以在其中放置所有代码。
  • 您可以在您的 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;});
    
  • 在您的ashx.cs文件中,您可以使用以下方法访问查询参数:

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

  • 然后,您可以使用以下方法返回结果:

    context.Response.Write(yourResponseString);