Monday, June 11, 2012

ColdFusion 10 and AJAX HTTP POST Request hangs

For the past few days I was working on an old application that now runs on ColdFusion 10 in my test environment and all the Ajax calls where just hanging with no response. The funny thing, is in the production server which is CF 8 everything was working fine and in 9 they worked as well as my development server was 9 before. After a little debugging I noticed the problem was because we had the calls being sent as HTTP POST request rather than GET. Now I remember the reason for doing this was an IE issue with caching that would be fixed by making the calls as POST rather than GET.

So how come ColdFusion 10 fails, well it is quite weird. First I will show you an example of how our calls where structured.

First our test CFC (ajax.cfc) we will use an example.


As you can see it is quite simple. We have a function called TestCall which requires an argument called name and then returns a struct with the name value back in it. Real simple just for this simple test.

Now below is an example of how our calls were structured.


As you can see we saved our CFC's url into a global variable in case we used it more than once, then our params in an object that get passed in the simple jQuery ajax function. At success I am just dumping our response to the console log. Absolutely nothing wrong here but in ColdFusion 10 this would just hang. Below is an example with 3 different calls and the firebug output for a better visual representation.




As you can see our first call is just hanging and honestly never responds and the following 2 do work. The difference between each, is that the second request is made as a GET HTTP request and the third is a POST but with the method name in the URL portion rather than as a POST param. So to me it appeared that CF is now only looking for a method definition in the GET (URL) scope and if passed in the POST (form) scope, it just didn't know what to do. As you can see I thought I had it solved but then one thing happened by mistake. As I was writing this post I forgot to add the wsdl to the URL and everything worked.




This time everything worked. Now when I started playing with AJAX a couple of years back it was my understanding that we had to append WSDL to our CFC request but it appears it is not needed and by removing this everything worked fine in 10. So if you are experiencing this same issue do 1 of 2 things.

  1. Remove WSDL from your URL if you have it
  2. Make sure the method request is in the URL portion in a POST request

Hope this helps someone from pulling out as many hairs as I did .. good night world!

3 comments:

  1. Anonymous2:45 AM












    variables.params = arguments.params;


    return getMetadata(variables.params).getName();









    $(function(){



    $.ajax({

    type : 'GET',
    data : {

    method : "getRemoteData",
    params : "Calling Remotely"


    },
    url : './com/remote.cfc',
    dataType : 'json',
    success : function(data){

    console.log(data);

    }


    });

    });


    Output?

    Response =>

    java.lang.String

    ReplyDelete
  2. Arghhh ... blogger killed my code ... :-)

    ReplyDelete
  3. Edward I do not understand your comment? Your call is doing a GET which works fine, the issue was with a POST call.

    ReplyDelete