Aspx页面内容正在添加到json resoponse中 [英] Aspx page content is getting added to the json resoponse

查看:107
本文介绍了Aspx页面内容正在添加到json resoponse中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有aspx页面,后面是代码隐藏文件。

在aspx页面中我有一些html内容和java脚本。

我向aspx页面发出请求在aspx的page_load中我正在做一些操作并将响应作为Response.Write(响应字符串)返回。

我观察到它将错误作为无效的json和响应体中的错误aspx内容随着我想要的回复而被添加。



我知道我做错了什么?



我的尝试:



我的Test.aspx.vb页面

=== =====================

I have aspx page with the code behind file.
In the aspx page i have some html content and java script.
I making a request to the aspx page in the page_load in the aspx i am doing some operation and returning the response as Response.Write("response string").
I observed that it's throwing error as invalid json and in the response body i total aspx content is getting added along with the response i want to throw.

Any idea what i am doing wrong?

What I have tried:

my Test.aspx.vb page
========================

Partial Class HostBudget
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        result = singleCopy.ValidateTemp()
        Response.Write(JsonConvert.SerializeObject(result))
    End Sub
End Class





我的Test.aspx页面包含

Html内容和javascript函数。



My Test.aspx page contains
Html content and javascript functions.

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Test.aspx.vb"

    Inherits="HostBudget" %>

<!DOCTYPE html>
<html>
<head  runat="server">
</head

<body></body></html>
<script>
Some js functions.
</script>





我的ajax函数调用

============= ===========



My ajax function call
========================

$.ajax({
      type: "GET",
      url: "../../HostBudget/Test.aspx,
      dataType: "json",
      success: function (response) {
          if (response.length > 0)
              alert(response)

      },
      error: function(XMLHttpRequest, textStatus, errorThrown) {
          CheckForAjaxError(XMLHttpRequest);
      }
  });





它将返回响应以及aspx页面内容。错误它作为无效的Json投掷



我知道我做错了什么?



It's returning the response along with the aspx page content. error it's throwing as Invalid Json

Any idea what i am doing wrong?

推荐答案

.ajax({
type:GET,
url:../../ HotBudget / Test.aspx,
dataType:json,
suc cess:function(response){
if(response.length> 0)
alert(响应)

},
错误:function(XMLHttpRequest,textStatus,errorThrown){
CheckForAjaxError(XMLHttpRequest);
}
});
.ajax({ type: "GET", url: "../../HostBudget/Test.aspx, dataType: "json", success: function (response) { if (response.length > 0) alert(response) }, error: function(XMLHttpRequest, textStatus, errorThrown) { CheckForAjaxError(XMLHttpRequest); } });





它将返回响应以及aspx页面内容。错误它作为无效的Json投掷



我知道我做错了什么?



It's returning the response along with the aspx page content. error it's throwing as Invalid Json

Any idea what i am doing wrong?


在response.write之前你返回你的json ,致电response.clear。现在,您的页面返回整页(这是正常行为),您只是将您的json添加到响应中。 Clear将删除除了你在清除后写的json之外的一切。



如果你需要页面显示AND和json,你必须将json放入脚本标记中以前定义的javascript变量。然后,您只能访问该部分,而不会在页面上显示。



来自MSDN:

Clear方法删除任何缓冲的HTML输出。但是,Clear方法仅删除响应体;它不会删除响应头。您可以使用此方法来处理错误情况。请注意,如果Response.Buffer未设置为TRUE,则此方法会导致运行时错误。
Before response.write where you return your json, call response.clear. Right now, your page returns full page (which is normal behaviour), you're just adding your json to the response. Clear will remove everything EXCEPT your json which you write after clear.

If you need the page to show AND to have json, you have to put json into script tag into previously defined javascript variable. Then you can access only that part without it being visible on the page.

From MSDN:
The Clear method erases any buffered HTML output. However, the Clear method erases only the response body; it does not erase response headers. You can use this method to handle error cases. Note that this method causes a run-time error if Response.Buffer has not been set to TRUE.


这篇关于Aspx页面内容正在添加到json resoponse中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆