0

I have an ASP.NET application sending data through AJAX to a handler withing my application. This works as it should when debugging locally, but as soon as I deploy the solution to the server, the handler only receives an empty string. I tried fiddling around with contentType and dataType, but without luck.

Here is my code so far.

aspx of the sending page, while "myData" is a simple string:

$.ajax({
    type: "POST",
    url: "handlers/changeRiskGroup.ashx",

    data: myData,

    // tried all those content/dataTypes without any luck
    //contentType: "text/plain",
    //dataType: "text",
    //contentType: "application/json; charset=utf-8",
    //dataType: "json",

    error: function (xhr, status, error) {
        console.log(xhr.responseText);
    },

    success: function (msg) {
        console.log(msg);
    }
});

.ashx.cs of the receiving handler:

public void ProcessRequest(HttpContext context) {
    //string data = new StreamReader(context.Request.InputStream).ReadToEnd();

    var data = String.Empty;

    context.Request.InputStream.Position = 0;
    using(var inputStream = new StreamReader(context.Request.InputStream)) {
        data = inputStream.ReadToEnd();
    }


    if (data != "") {
        // doing something with my data here. 

        // this is never reached while on the server, but works fine locally!
    } else {
        context.Response.Write("Please supply data to the risk group service!");
    }
}
public bool IsReusable {
    get {
        return false;
    }
}

}

The data variable in the .ashx.cs file is filled when debugging locally, but always "" on the server. I have no clue why.

6
  • Have you verified whether the client (Jquery) is sending the same data to the deployed version using Fiddler? Commented Aug 17, 2015 at 9:40
  • is your local setup identical with the one on the server? if you're using same code and same browser, it's feasible that the server you're working with it configured differently and that's what needs updating or fiddling around with Commented Aug 17, 2015 at 9:41
  • can you show an example for the value of myData? Commented Aug 17, 2015 at 9:49
  • @Mendhak - yes the sending page has the right data, I just verified using fiddler. Commented Aug 17, 2015 at 11:01
  • @ZathrusWriter - so far yes. What would be setup topics that affect POSTs to the server? Commented Aug 17, 2015 at 11:02

2 Answers 2

1
var para={};
para.myData="abcd"

$.ajax({
    type: "POST",
    url: "handlers/changeRiskGroup.ashx",
    data: para,

    error: function (xhr, status, error) {
        console.log(xhr.responseText);
    },
    success: function (msg) {
        console.log(msg);
    }
});

from server side

string myData=contect.Request.Form["myData"].toString();
Sign up to request clarification or add additional context in comments.

4 Comments

Tried your solution, works fine locally. When deployed on the server Request.Form["myData"] is null.
If it is working on local then it must work on Server. May be URL and post will be different.
Thats also what I'm thinking - it is basically right, but somewhere there's a probably tiny problem. The URL of the handler was an earlier problem ('/handlers/myhandler.ashx' works locally but not on the server, changed it to 'handlers/myhandler.ashx' which does work). If nobody has a clue I will set up a testing page + handler to try and narrow down the problem - but this means lots of work, therefore I was hoping someone faced that (trivial???) problem before.
.. is this really an unsolvable problem?!
0

Easy, just took me ~20 hours to figure out. Found the answer here: Web service returns "301 error moved permanently" in production environment

In short, I created a blank page within my project to ensure no plugins etc were interfering with the jQuery execution. Further, I created a very simple mask to submit certain data to the handler URL. Within this mask I varied different ways to POST data, when I tried implementing the POST as a [WebMethod] I finally got a clue, as the response was "301 moved permanently" from the WebMethod. Therefore I could start investigating and found out that my server was lowercasing the urls, obviously jQuery/HTTP does not like that.

I hope this post helps others struggling with similar problems.

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.