CFAjax

 

CFAjax function only allowing HTTP Post request

Example

Select a State :

Whats happening?

In this example CFAjax function is only accepting HTTP Post requests, so if the client tried to make a HTTP GET request they end up getting and error alert. Try click on both the button on this example to see the results.

How does this example works?

When HTTP Post button is clicked btn_httpPost_clicked() JavaScript function gets called, which makes DWREngine to use HTTP POST for making CFAjax calls. Similarly when btn_httpGet_clicked() function is called it makes DWREngine to use HTTP GET method for making CFAjax calls. Once the method verb has been set getCounty() function is called to make the CFAjax call.
	function btn_httpPost_clicked()
	{
		DWREngine.setVerb("POST");
		getCounty();
	}

	function btn_httpGet_clicked()
	{
		DWREngine.setVerb("GET");
		getCounty();
	}
	
					
The getCounty function looks like this:
	function getCounty()
	{
		var state = DWRUtil.getValue("state");
		DWREngine._execute(_cfscriptLocation, null, 'stateinfoHttpMethodCheck', state, getCountyResult);
	}
	
getCounty function first gets the value of state selected , and then calls DWREngine._execute method which in turn calls stateinfoHttpMethodCheck coldfusion function with the parameter as state code. The DWREngine parameters are explained below:
	_cfscriptLocation -> location of the coldfusion file that has the function implementation
	null, -> Default value
	'stateinfoHttpMethodCheck', -> Coldfusion function to be called
	state -> State code for which we need the county listing
	getCountyResult, -> Javascript function that will be called after countylookup CF function has been executed
					
Coldfusion function stateinfoHttpMethodCheck takes one argument state and return the information related to that state. But the important information to look at is the hint section “ hint="httpRequestMethodAllowed='POST'" This function is putting a constrain that only request that originated from HTTP POST method will be allowed and HTTP GET requests will be rejected. Listed below is the CF code:
<cffunction name="stateinfoHttpMethodCheck" hint="httpRequestMethodAllowed='POST'">
	<cfargument name="state" required="yes" type="string">
	<cfreturn getStaticStateInfoString(state=arguments.state)>
</cffunction>
		

IMPORTANT
To allow HTTP GET use httpRequestMethodAllowed='GET'
To allow HTTP POST use httpRequestMethodAllowed='POST'
To allow Both HTTP POST and GET dont use this hint attirbute at all Or optionally you can use httpRequestMethodAllowed='GET,POST'

If the client makes a HTTP Request using one of the method that is not allowed in CFAjax function, the client will see alert box saying Request Denied. This is the default message that shows up. But you can always override the message and present the customer with your own custom made message or don’t show anything at all. To override the default authentication fail message you can define a javascript function in your code like this.

function myErrorHandler(message)
{
	alert(‘not allowed here, go play poker!’);
}

DWREngine.setHttpRequestMethodDeniedHandler(myErrorHandler);
Now anytime http request is denied, a JavaScript alert will show up with the message ‘not allowed here, go play poker!’

When the coldfusion method returns data, getCountyResult JavaScript function gets executed with displays the text in the info span:

	function getCountyResult(result)
	{
		document.getElementById("info").innerHTML = result;
	}