ColdFusion & jquery asynchronous Service Proxy

Source: Internet
Author: User

ColdFusion cfmproxy. cfm code:

/*** @ Hint returns the request object submitted by Ajax * @ returntype any */function getajaxrequest () {var content = getrequestcontent (); if (content = "" & isdefined ("URL. P ") {content = URL. p;} If (isjson (content) {return deserializejson (content) ;}else {return "";}}

/*** @ Hint returned page request content * @ returntype string */function getrequestcontent () {var result = ""; var httprequest = getpagecontext (). getrequest (); var contentlength = httprequest. getcontentlength (); If (contentlength GT 0) {bytearray = getbytearray (contentlength); httprequest. getinputstream (). readline (bytearray, 0, contentlength); Result = tostring (Createobject ("Java", "Java. lang. string "). init (bytearray);} return result ;}

<cfprocessingdirective pageencoding="utf-8" />
<Cftry> <cfscript> cfmresult ={}; cfmresult. success = false; cfminfo = $ utils. getajaxrequest (); </cfscript> <cfif isstruct (cfminfo) and structkeyexists (cfminfo, "cfmpage") and structkeyexists (cfminfo, "method")> <cfinclude template = "# cfminfo. cfmpage # "/> <cfscript> If (structkeyexists (variables, cfminfo. method) {method = Variables [cfminfo. method]; If (isdefined ("cfminfo. arguments ") {cfmresult. data = method (argumentcollection = cfminfo. arguments);} else {cfmresult. data = method ();} cfmresult. success = true;} else {cfmresult. data = "the requested method does not exist" ;}</cfscript> <cfelse> <cfset cfmresult. data = "request format error"> </cfif> <cfcatch type = "any"> <cfset cfmresult. data = cfcatch. message/> <cfif _ $ returnexceptiondetail {1} gt; <cfset cfmresult. exception = cfcatch> </cfif> </cfcatch> </cftry> <cfcontent type = "application/JSON; charset = UTF-8"/> <cfoutput> # serializejson (cfmresult) # </cfoutput>

Client jquery code:

    _postCFProxy: function (request) {        var dtd = jQuery.Deferred();        jQuery.ajax({            type: "POST",            url: request.url,            contentType: "application/json",            data: jQuery.toJSON(request)        })        .done(function (data, textStatus, jqXHR) {            if (data.SUCCESS) {                dtd.resolve(data.DATA);            }            else {                dtd.reject(data);            }        })        .fail(function (jqXHR, textStatus, errorThrown) {            dtd.reject({ DATA: jqXHR.responseText }, jqXHR);        });        return dtd.promise();    },    _callcfm: function (cfm, method, params) {        var cfminfo = {            url: "cfmproxy.cfm",            cfmpage: cfm,            method: method        };        if (params) {            cfminfo.arguments = params;        }        return this._postCFProxy(cfminfo);    }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.