Using cfhttp to post data
On my site right now, I'm passing in data through a query string to a page. I'd like to change that so that the data is passed as a POST parameter.
My previous statement looked like this:
<cf_location url="mypage.cfm?id=123">
And I replaced it with:
<cfhttp method="post" url="mypage.cfm">
<cfhttpparam name="theID" type="URL" value="123">
</cfhttp>
But cfhttp isn't posting anything. In Firebug, nothing shows up in the NET tab, and nothing happens when that code is supposed to run.
Am I doing this incorrectly? Am I using the right type in the cfhttpparam? I'm very new to ColdFusion, so this is diffic开发者_开发技巧ult for me.
Assuming you want the server, and not the client, to see the result, you can use CFHTTP. As Edward pointed out, the client will never see the interaction, as it is only between the server and (in your example) itself (although you need a fully qualified URL, including host name or IP).
In your example, there are a few things wrong:
- you need a fully qualified URL
- you are using URL variables, not FORM (formField) variables
This, with a bit of tweaking (specifically the URL) should work:
<cfhttp method="post" url="http://localhost/mypage.cfm">
<cfhttpparam name="theID" type="formField" value="123">
</cfhttp>
Remember, the response is coming back to the server, so the end user will never see mypage.cfm. The response will be returned to the server in the CFHTTP variable (you can change that using the "result" attribute).
Unless you don't have control of mypage.cfm, it might be easier to edit it to take URL variables, or to use structAppend() to copy the URL variables to the FORM scope.
If you are just passing data from one page to another, your options are in the URL, in a form field (possibly a hidden field), in a cookie, or via a session variable.
One way to do it (depending on what you are trying to do) is (with sessions variables set on in your application)
in page1.cfm
<cfset session.theid="123">
<cf_location url="page2.cfm">
then on page2.cfm
<cfoutput>theid=#session.theid#</cfoutput>
but consider multiuser situation in terms of overwrites with concurrent access.
The CFLOCATION tag issues a redirect to the browser, telling it to go to a different url.
CFHTTP tells the CF SERVER to make an http connection to another server.
They are not in any way comparable operations.
If you're trying to post data from a browser to the CF server, then you need to use the HTML FORM tag.
Post Comments:
What CFHTTP does is make an http connection from the server to another server. So, what you'd be doing is making an HTTP connection from your own server to your own server, which, unless you're developing a hardcore SOA system might be a little bit odd.
Normally, since you're already at the server, what you should be doing is coding the functionality that you need on both pages into a reusable component (a cfc or even just a user-defined-function), and then invoke that in both pages.
But, if you really do what to do what you're asking, then, what you've put here is correct.
One things you'll need to change is the URL needs to be fully qualified (ie. myserver.mydomain.com/myapp/mypage.cfm). You can't use 'relative' urls.
You won't see anything in firebug because its going from the SERVER to the SERVER, and the browser's not involved at all.
Using cfhttp
is just for another location out of your application. Regarding your message, you use cfhttp
for submitting value into same application.
精彩评论