0

In my ASP.NET page I have the following method:

public static void UpdatePage(string accessCode, string newURL)
{
    HttpContext.Current.Cache[accessCode] = newURL;
}

It actually should receive the accessCode and newURL and update the Cache accordingly. I want to pass the values to that method from JavaScript, using an AJAX request. The code for it is as follows:

function sendUpdate() {
    var code = jsGetQueryString("code"); 
    var url = $("#url_field").val();
    var dataToSend = [ {accessCode: code, newURL: url} ];
    var options = { error: function(msg) { alert(msg.d); },
                        type: "POST", url: "lite_host.aspx/UpdatePage",
                        data: {"items":dataToSend}, 
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        async: false, 
                        success: function(response) { var results = response.d; } }; 
        $.ajax(options);
}

However this doesn't seem to work. Could anybody help me figure out where the bug is?

1 Answer 1

1

UpdatePage is a void method that doesn't return anything, so there is nothing to look at in the response.

You could look at the HTTP return code and check that it was 200 OK or you could modify the web method:

public static bool UpdatePage(string accessCode, string newURL)
{
    bool result = true;
    try {
       HttpContext.Current.Cache[accessCode] = newURL;
    }
    catch {
        result = false;
    }

    return result
}

Edit:

It looks like your JSON arguments to the WebMethod are incorrect, you don't need the "items" in your JSON. The arguments should match your webmethod exactly.

function sendUpdate() {
       var code = jsGetQueryString("code"); 
       var url = $("#url_field").val();
       var options = { error: function(msg) { alert(msg.d); },
                       type: "POST", url: "lite_host.aspx/UpdatePage",
                       data: {'accessCode': code, 'newURL': url}, 
                       contentType: "application/json; charset=utf-8",
                       dataType: "json",
                       async: false, 
                       success: function(response) { var results = response.d; } }; 
       $.ajax(options);
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for your response. I actually do not want that method to return anything. It just needs to update a value in the cache.
@cycero I've updated the response, your .ajax options need to be adjusted.
Thanks for your help. But I'm getting an alert with "undefined" when calling the sendUpdate() function. That's actually the alert called in error case of the AJAX request.
Thanks @dmck. The problem was actually in the form of the JSON string I was passing to the WebMethod. I had to send it in this form instead: data: "{ accessCode: " + code + ", newURL: '" + url + "' }",

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.