Click here to Skip to main content
15,908,111 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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
========================
VB
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


My Test.aspx page contains
Html content and javascript functions.
ASP.NET
<%@ 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>


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);
      }
  });


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?
Posted
Updated 23-Mar-16 21:49pm
v4
Comments
Richard Deeming 23-Mar-16 16:15pm    
There's a mistake somewhere in your code. Unfortunately, you chose not to show us any of your code, so we can't tell you what that mistake is, or how to fix it.

Click "Improve question" and update your question with the relevant parts of your code, and the full details of any errors.
MYQueries1 23-Mar-16 16:27pm    
I have updated the question please help
Mathi Mani 23-Mar-16 16:40pm    
You are calling Test.aspx and you are getting the page content. It seems like it is working correct. May be if that is not what you want, then change the URL to the correct one.
MYQueries1 23-Mar-16 18:12pm    
I just want to return and variable to the my ajax call not total page content how i can do that?

1 solution

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.
 
Share this answer
 
Comments
MYQueries1 24-Mar-16 13:59pm    
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.

How i Can do this?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900