tags:

views:

1170

answers:

5

I'm trying to pass JSON from jQuery to a .ASHX file. Example of the jQuery below:

$.ajax({
      type: "POST",
      url: "/test.ashx",
      data: "{'file':'dave', 'type':'ward'}",
      contentType: "application/json; charset=utf-8",
      dataType: "json",      
    });

How do I retrieve the JSON data in my .ASHX file? I have the method:

public void ProcessRequest(HttpContext context)

but I can't find the JSON values in the request.

Thanks in advance.

A: 

Try System.Web.Script.Serialization.JavaScriptSerializer - with casting to dictionary

Dewfy
Thanks for the reply. Could you elaborate a little please? What object should I be serializing?
colin jobes
@colin, I'm preferring Dictionary, but you can persist any object, for example in your sample: class MyObj{ public String file, type; }
Dewfy
+1  A: 

Hello!

If you send data to the server with respect of $.ajax the data will not be converted to JSON data automatically (see http://stackoverflow.com/questions/2737525/how-do-i-build-a-json-object-to-send-to-an-ajax-webservice/2738086#2738086). So you can use contentType: "application/json; charset=utf-8" and dataType: "json" and stay don't convert data with JSON.stringify or $.toJSON. Instead of

data: "{'file':'dave', 'type':'ward'}"

(manual converting of data to JSON) you can try use

data: {file:'dave', type:'ward'}

and get the data on the server side with context.Request.QueryString["file"] and context.Request.QueryString["type"] constructs. If you do receive some problems with this way then you could try with

data: {file:JSON.stringify(fileValue), type:JSON.stringify(typeValue)}

and usage DataContractJsonSerializer on the server side.

Oleg
Thanks for the reply. The issue I'm having is just that I can't get the JSON data into the request object when using an ASHX page. The value of context.Request.QueryString["file"] is always null. Would you know how to get the JSON data into the request?
colin jobes
To be able to see `file` parameter with `context.Request.QueryString["file"]` you should use `data` like `data: {file:'dave', type:'ward'}` (see my answer). Then query parameters with the names `file` and `type` will be defined and the data posted to server will be encoded like `file=dave?type=ward`.
Oleg
If your AJAX is POSTing, then the data will be in the Request.Form property, not QueryString.
dave thieben
@dave thieben: You are right. Thank you for the advice. I don't use ASHX myself. So instead of `Request.QueryString` the `Request.Form` would be better. It seems to me that `Request.Params` is the best way because it will work for both GET and POST (see http://msdn.microsoft.com/en-us/library/system.web.httprequest.params.aspx)
Oleg
A: 

if using $.ajax and using .ashx to get querystring ,dont set datatype

$.ajax({ 
    type: "POST", 
    url: "/test.ashx", 
    data: {'file':'dave', 'type':'ward'}, 
    **//contentType: "application/json; charset=utf-8",   
    //dataType: "json"**    
}); 

i get it work!

A: 

This works for calling web services. Not sure about .ASHX

$.ajax({ 
    type: "POST", 
    url: "/test.asmx/SomeWebMethodName", 
    data: {'file':'dave', 'type':'ward'}, 
    contentType: "application/json; charset=utf-8",   
    dataType: "json",
    success: function(msg) {
      $('#Status').html(msg.d);
    },
    error: function(xhr, status, error) {
        var err = eval("(" + xhr.responseText + ")");
        alert('Error: ' + err.Message);
    }
}); 



[WebMethod]
public string SomeWebMethodName(string file, string type)
{
    // do something
    return "some status message";
}
Larry Flewwelling
A: 

you have to defined the handler properties in web configuration file to handle the user defined extension request formats. here the user defined extension is ".api"

add verb="*" path="test.api" type="test" replace the url: "/test.ashx" to url: "/test.api" .