0

How to return some value from asp.net page that is called using jQuery Get() method ? The get method is given below and i want the alert() should display the returned value.

The get() calls "result.aspx" page that return a string and that string i want to show in alert().

$.get("result.aspx",
    {
        name: "Donald Duck",
        city: "India"
    },
    function (result, status, xhr) {
        alert(result);
    }
);

The result.aspx code is:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        LoadReturn();
    }
}
void LoadReturn()
{
    string name = Request.QueryString["name"];
    string city = Request.QueryString["city"];
    string returnValue="Hello Mr."+ name + " who lives in " + city;

    //How to return value of returnValue to the jquery get() function. 

}

How to return value of returnValue to the jquery get() function ??

Update I tried the response.write(). It is able to return the data to jquery get() method.

Response.Write(returnValue);

Only problem is that it is also sending me the full HTML of the result.aspx page. Which i don't want. enter image description here How can i prevent the unnecessary HTML from reaching the jquery get() method?

4
  • 3
    use a WebMethod or a separate .svc webservice file. aspx pages are designed to serve a whole HTML page from a full page load, not fragments of HTML or JSON to an ajax call. This tutorial gives a nice simple example using a WebMethod. aspsnippets.com/Articles/… Commented Jan 12, 2017 at 10:15
  • i know about the .ajax() method but how to return with .get() in asp.net? Commented Jan 12, 2017 at 10:21
  • 2
    Have you tried Response.End() following Response.Write() ? ADysons answer is probably the correct way forward if you're not just looking for a quick fix though. Commented Jan 12, 2017 at 10:48
  • @gb2d i haven't tried the Response.End();. I did it now the extra unnecessary page HTML is gone. Problem resolved. Thank you. Commented Jan 12, 2017 at 10:54

1 Answer 1

1

I solved it by putting Response.End() in the end. Hope it helps others too.

void LoadReturn()
{
    string name = Request.QueryString["name"];
    string city = Request.QueryString["city"];
    string returnValue="Hello Mr."+ name + " who lives in " + city;

    Response.Write(returnValue);
    Response.End();
}
Sign up to request clarification or add additional context in comments.

1 Comment

this definitely helped me as well!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.