JQuery POST. Unable to get request parameters using custom httphandler

I have a jQuery post method with JSON data included.

In my httphandler, in the processRequest method, Request ["Operation"] is null and none of my data has been sent. I work in a SharePoint 2010 environment.

public void ProcessRequest(HttpContext context) { try { string operation = context.Request["Operation"]; // Returns null 

My JavaScript looks like this:

 function CallService(serviceData, callBack) { $.ajax({ type: "POST", url: ServiceUrl, data: { Operation : "activate"}, contentType: "application/json; charset=utf-8", dataType: "json", success: function (result) { callBack(result); }, error: function (XMLHttpRequest, textStatus, errorThrown) { alert(XMLHttpRequest.responseText); } }); 

In the debugger in VS, I cannot find the published values โ€‹โ€‹when I evaluate the HttpContext. In Firebug, the value is published as valid JSON data. Any reason why I cannot get the parameters?

Any help was appreciated.

+4
source share
4 answers

Thank you all for participating. I decided instead to read the request input stream and get a pair of key values. I can access all my options in this way.

I also use the $ .toJSON () function to pass my parameters to an Ajax call. The JsonConvert class is the JSON.Net assembly from Newtonsoft. I use it a lot and highly recommend using it if you use any json serialization materials.

By the way, changing the quotes around the input parameters really worked. I want to continue to use one common ajax function and use the $ .toJSON function and generally pass an object with all my parameters as post data.

 TextReader reader = new StreamReader(context.Request.InputStream); Dictionary<string, string> requestParams = JsonConvert.DeserializeObject<Dictionary<string, string>>(reader.ReadToEnd()); try { switch (requestParams["operation"]) 
+1
source

I changed the contentType to application/x-www-form-urlencoded and it did the trick

+1
source

Perhaps you are limited by the same policy of origin . Is ServiceUrl the same name and domain as the calling page?

0
source

Why are you overriding the contentType parameter in your call to $.ajax() ? If you omit this, do you still see that null is sent for the value of Operation ?

In addition, I believe that the correct formatting of JSON data will be:

 {"Operation": "activate"} 

I think the JSON specification is specific to this, but most frameworks are not so strict.

0
source

Source: https://habr.com/ru/post/1315693/


All Articles