[SalesForce] Ajax Request Returns Nothing

I'm trying to post some data via ajax and get a response back indicating if the post worked correctly, however I'm running into some issues.

I have a visualforce page called updateLog (/apex/updateLog) and it has the folliwing markup:

<apex:page controller="WatcherController" contentType="text/javascript" showHeader="false" action="{!updateWatcherLog}">{!updateWatcherLogResponse}</apex:page>

updateWatcherLogResponse is a JSON serialized Map of values that I would like returned as a response to the ajax request.

The following is the javascript from which I'm making the ajax request:

console.log("about to send log...");
var params = {"page":window.location.href};
var request = new XMLHttpRequest();
request.onreadystatechange = function() {  
  console.log(request.readyState);  
  if (request.readyState == 4) {
    console.log("request finished, response: " + request.response);    
  } else {
    console.log("request state changed to: " + request.readyState);
  }
}
request.open("POST","/apex/updateLog",true);
request.setRequestHeader("Content-Type", "application/json");
request.send(JSON.stringify(params));

When readyState equals 4 (The request is finished), I'm printing out the returned response, which always is an empty string. As a side note, whenever I set onerror for the request, the function seems to run very quickly, indicating that there may be an error with the way I'm setting it up and the request might not be sent at all (My System.debug log statements are also not being hit whenever I make a request using the above javascript as further evidence of this).

I can't use jQuery, Dojo, or any other 3rd party libraries for this, and RemoteActions are not a possibility. Any help would be greatly appreciated!

EDIT:

Here is a sanitized version of the controller

public class WatcherController {

    public String updateWatcherLogResponse {get; set;}

    public WatcherController() {
        //Not important
    }

    public void updateWatcherLog() {
        String page = ApexPages.currentPage().getParameters().get('page');
        System.debug('got here, page equals: ' + page);
        System.debug(ApexPages.currentPage().getParameters());
        Map<String,boolean> result = new map<String,boolean>();
        try {
            User currUser = [SELECT Id FROM User WHERE id =: UserInfo.getUserId()];
            currUser.Last_Page_Viewed__c = page;
            currUser.Last_Page_Viewed_Date_Time__c = DateTime.now();
            update currUser;
            result.put('result',true);
        } catch(Exception ex) {
            System.debug(ex.getMessage());
            result.put('result',false);
        }
        updateWatcherLogResponse = JSON.serialize(result);                                     
    }
}

Best Answer

Have you tried a commandLink with parameters (and reRender if needed) to achieve your goals? While this doesn't answer your stated question of why the ajax doesn't work, it does give you a way to asynchronously send parameters and retrieve results without use of RemoteActions or 3rd party libraries.

If, you need to use this function programaticaly, there is always actionFunction. This gives you a javascript method you can call like any other.

Related Topic