How to set 'status'-parameter of the $.post()-function in aspx-response?
<p>I have a HTML-page with some JS-code in it, which is loaded from the Android <code>WebView</code>. This JavaScript uses the <a href="http://api.jquery.com/jQuery.post/" rel="nofollow">jQuery-post-function</a> to call the <code>HandleAndroidJSON.aspx</code>-site with data, and return a callback-function with a <code>data</code>-, <code>status</code>- (and <code>jqXhr</code>-) parameter:</p>

<pre><code>function syncWithServer() {
    var dataToSend = Android.getDataToSend();
    $.post("HandleAndroidJSON.aspx", { data: dataToSend }, callbackDataFromServer);
}

function callbackDataFromServer(data, status) {
    Android.setDataFromServer(data, status);
}
</code></pre>

<p>The "Android"-variable is a <code>JavaScriptInterface</code>created in Android that makes it possible to call Android-methods from JS.</p>

<p>In my <code>HandleAndroidJSON.aspx.cs</code>-file I write a response depending on the data I receive from Android:</p>

<pre><code>public partial class HandleAndroidJSON : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        Response.Clear();
        Response.ContentType = "text/plain";

        // Read request parameter and deserialize JSON data.
        string jsonDataFromClient = Request.Params["data"];

        // ... Do stuff with the data ...

        if (error != null)
        {
            //This goes into the data-parameter of the callbackFunction
            Response.Write(error); 
            //Response.Status = "403 Error"; //This seems to only be HTTP-status responses,
                                   //not the one used as parameter in the callback-function
        }
        else
        {
            //This does too
            Response.Write(jsonDataToSend); 
        }
        Response.End();
    }
}
</code></pre>

<p>The <code>Response.Write()</code>-method returns in the the <code>data</code>-parameter in the callback-function as intended, but I cannot seem to set the <code>status</code>-parameter - <strong>it always returns the same string</strong>: "success" and doesn't change if I set the <code>Response.Status</code>-property.</p>

<p>So the question is simply, how do I set the <code>status</code>-parameter of the <code>callbackDataFromServer</code>-function (that is called from the jQuery-post-function) from the aspx.cs-code?</p>

<p><strong>EDIT</strong>: I'm not talking about <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html" rel="nofollow">status-codes for HTTP</a>. Setting the <code>StatusDescription</code>-, <code>StatusCode</code>- and the <code>Status</code>- properties, of the <code>Response</code>-property, doesn't change the <code>status</code>-parameter of the callback-function.</p>

<p><strong>EDIT2</strong>: I don't think handling the status-parameter in the JavaScript-code solves the problem because I would have to validate the data in Android again anyway (since JavaScript-injection would be possible to do to avoid the error handling). I would like to pass it on to the Android-method via the status-parameter, in the <code>callbackDataFromServer</code>-function, as the current code indicates.</p>

<p><strong>EDIT3</strong>: Even though I can't seem to find a straight answer to whether it's possible to set the <code>status</code>-parameter from the aspx-code, it seems that the only thing you can change beside the <code>data</code>-parameter is the <code>jqXhr.status</code> property, which can be set be the <code>Response.StatusCode</code> property in the aspx-site.</p>

<p>There's also the <code>jqXhr.statusText</code> property which holds a string, but it seems to be the default text, based on the statuscode, and not the <code>Response.StatusDescription</code> (setting the statusCode to 200 and the statusDescription to "success" returned as "OK", which is default for the statusCode 200).</p>

<p>This is the topic I was reading: <a href="http://stackoverflow.com/questions/9949146/get-the-jqxhr-responsetext-on-a-jquery-getjson-request-using-jsonp">Get the jqXhr.responseText on a jQuery getJSON request using JSONP</a></p>

<p><strong>EDIT4</strong>: I now figured out that setting the <code>StatusCode</code> to a value, that implies something went wrong, will result in no data being sent, even though the <code>Response.Write()</code>-method has been called, which defeats the purpose I'm trying to achieve.</p>

<p>The proper question would then be: <strong>How can I properly pass on the error text (from ASP.NET, through jQuery) if something went wrong, instead of the data? And how can I ensure that the data I'm transfering is either an error or JSONData?</strong>
I figure the simple way is just to check if the data starts with a "{" or "[" but I feel like it's a bit of a hack.</p>
