1

I am new to ASP. I want to make an ASP page that would return on request JSON formatted data. I followed a few tutorials and all i can see now is the link of a full fledged page with callbacks in my code. So in essence the server is ouptputting a lot of HTML which obviously is not valid JSON. So my only question is how do I go about making such a web page. I want my application to be designed such that when I go to a web page, say http://localhost:8080 (or something similar), a callback or a function in my C# code connects to the database, gathers some info and sends it back as the Response in valid JSON. I'm not looking for libraries which can achieve the same effect, but something like how do I achieve it by simply using a set of Response.Write statements.

Please pardon me if I may give the idea that I am not coherent with my concepts, because I'm really new to this entire thing.

Any help is greatly appreciated.

Regards, p1n3appl3

EDIT: I'm currently using the following JavaScript code:

var xhr = new XMLHttpRequest();
xhr.open("POST", "http://localhost:47949/Default.aspx/NameChange");
xhr.onreadystatechange = function () {
    if (xhr.readyState == 4 && xhr.status == 200) {
        alert(xhr.responseText);
    }
}

xhr.send("");

It makes the call and all, everything's fine but the problem is that it returns the entire code of the "Default.aspx" page again. I mean, the method NameChange isn't even called (I've tested this by setting breakpoints).

What am I doing wrong?

2 Answers 2

3

You could define a PageMethod on your page:

public partial class _Default : Page 
{
    [WebMethod]
    public static SomeObject GetObject()
    {
        SomeObject result = ... fetch from db or something
        return result;
    }
}

or you could also do this manually using the JavaScriptSerializer class:

public partial class _Default : Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Clear();
        Response.ContentType = "application/json";
        SomeObject result = ... fetch from db or something
        var serializer = new JavaScriptSerializer();
        string json = serializer.Serialize(result);
        Response.Write(json);
    }
}
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks a lot for the prompt reply. I tried the above code, but it sort of is failing to work. I have a feeling this is because when requesting a URL [in my case, http://localhost/Default.aspx/GetObject] I'm just putting the URL in the browser's URL bar and this causes it to make the call using the HTTP GET method. Am I required to use POST instead. Because, currently, I'm just being redirected to the Default page.
@user837610, yes Page methods are only accessible with POST requests. You cannot invoke them directly with a GET request such as /Default.aspx/GetObject. You could try the second approach or write a custom generic handler.
Well, I'm mostly going to be using it from an XHR object itself, so it's not really a concern that GET requests don't work. anyways, thanks a lot for the help :)
Please check the edited question and see if you can help me with this.
@user837610, you need to set the proper request content type header to application/json. Look at the example I've linked which uses jquery: encosia.com/…
|
1
string jsonProperty = Request["JSonProperty"];//your result
if (!string.IsNullOrEmpty(jsonProperty))
{                    
    Response.Clear();
    Response.ContentType = "application/json";
    Response.Write(RenderJSon());

    Response.Flush();                    // Flush the data to browser
    Response.SuppressContent = true;     // Suppress further output - "standard" html-
                                         // content is not rendered after this

    return;
} 

Comments

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.