Search code examples
jqueryajaxcoldfusioncfcapplication.cfc

Can anyone help me figure out why I cannot access my cfc with an ajax request, but have no problem via browser?


I am working on a simple form using ColdFusion MX7. I have a pair of text inputs I want to populate based on a what is selected in a cfselect. Any Ajax calls to my CFC return a 404 error. If I access the CFC from my browser I get no such error. I made this using Ben Nadel's example of making a custom java proxy for a cfc. The cfc is located in the same folder as the cfm page that this script is on. Here is the pertinent code:

 function RemoteCFC(){
        this.name = " ";
        return( this );
       }


      // This handles the remote calls to the CFCs.
      RemoteCFC.prototype.MakeRemoteCall = function(strMethod, objData, fnSuccess, fnError){

      // Create a data struct and extend it with the method
      // name and the data to be passed.
      var objRemoteData = {};

      // Extend the remote data set.
      $.extend(objRemoteData, objData, {method: strMethod, returnFormat: "json"});
      // Make the AJAX call to the remote method.
      $.ajax({type: "get", url: (this.Name + ".cfc"), data: objRemoteData, dataType: "json", 
             success: fnSuccess, error: fnError});

      // Return this for method chaining.
      return( this );
   }

   // Create a new core remote object. We will need this
   // to create the prototype chain such that the other
   // proxy classes can extend this.
   objRemoteCFC = new RemoteCFC();
   // Create a Javascript proxy for this given CFC.
   function RecallService(){
      this.Name = "RecallCountFunctions";
   }

   // Extend the core CFC Proxy functionality.
   RecallService.prototype = objRemoteCFC;


    RecallService.prototype.getInspected = function( objData, fnSuccess, fnError ){
        this.MakeRemoteCall( "getInspected", objData, fnSuccess, fnError );
    }

    //Define another remote method wrapper. 
    RecallService.prototype.getHeld = function( objData, fnSuccess, fnError ){
        this.MakeRemoteCall( "getHeld", objData, fnSuccess, fnError );
    }

   $(   
      function(){
        var myRecallService = new RecallService();

        $( "select[ name = 'CountChosen' ]" ).change( function(){
           var selectedCount = $( "select[ name = 'CountChosen' ]" );
           var inspected = $("input[name='numInspected']");
           var held = $("input[name='numHeld']");

           if (selectedCount.val() != "0"){
               EnableForm();
               myRecallService.getInspected({store: "#cgi.AUTH_USER#", id: selectedCount}, function
                      ( objResponse ){SetValue("inspected", objResoponse );}, function( objResponse ){
                      alert( objResponse.responseText );});                     
               myRecallService.getHeld({store: "#cgi.AUTH_USER#", id: selectedCount}, function
                      ( objResponse ){SetValue("held", objResponse);}, function( objResponse ){
                      alert( objResponse.responseText);});                      
           }
       });
   }

Sorry about the long post, I didn't want to leave out annything that may help. I am also not using Application.cfc or an onRequest method which I heard may cause issues.

If you need to see my cfc let me know I can post it as well.

Fix was:

Change:
   id: selectedCount
To:
   id: selectedCount.val()

Solution

  • And besides Ray's question about what Firebug (or Charles or Fiddler or other proxies may tell you), which should certainly help if it's really just a 404 error, here are some other thoughts if it's not.

    First, when you say you can access it from your browser, do you mean as http://yourserver/yourcfc.cfc?method=methodname? Or something else? And since you show your ajax library adding returnformat=json, are you adding that on the URL as well to test?

    Second, when you say you're using no application.cfc or cfm, do you realize it's not just whether there's one in that directory, but any in the parent, grandparent, and so on? You could be influenced by one of those. The easiest way to avoid it (for testing purposes) is to drop a blank one into the directory of the page you're calling.

    Third, if you look in the application.log file (in [cf]\logs, or via the CF Admin), does it show an error happening? The Ajax client will typically hide that. Indeed, you mention getting a 404. Are you sure that's the CFC that's not being found? What's reporting that?

    And is the CFC in the same directory as the CF page that served up this HTML/javascript? Since you only name the file, it would seem it would be looking there. Were you testing that with your sample URL in the browser?