如何在 CFWheels, for Ajax 中启动控制器/操作

How to initiate controller/action in CFWheels, for Ajax?

本文关键字:启动 控制器 操作 Ajax for CFWheels      更新时间:2023-09-26

我已经尝试让cfwheels与FineUploader一起工作了两天,但我只是不知道如何强制jQuery脚本执行控制器/操作。这就是我到目前为止所走的路:

$(document).ready(function() {
    var restricteduploader = new qq.FineUploader({
        // Since we're using jQuery, use the jQuery way to select the HTML element
        element: $('##restricted-fine-uploader')[0],
        request: {
            type: "POST",
            url: $(this).attr("href") + "/index.cfm?controller=users&action=UploadFileXhr&format=json", // References "/say/hello?format=json"
            dataType: "json",
            endpoint: '/index.cfm?controller=users&action=UploadFileXhr&format=json'
        },
        multiple: false,
        validation: {
            allowedExtensions: ['jpeg', 'jpg', 'txt'],
            sizeLimit: 5120000000 // 50 kB = 50 * 1024 bytes
        },
        text: {
            uploadButton: 'Click or Drop'
        },
        showMessage: function(message) {
            // Using Twitter Bootstrap's classes and jQuery selector and method
            $('##restricted-fine-uploader').append('<div class="alert alert-error">' + message + '</div>');
        },
        debug: true
    });
});

CFWheels文档说我必须使用它来获取异步请求:

(function($){$(document).ready(function(){
    // Listen to the "click" event of the "alert-button" link and make an AJAX request
    $("#alert-button").click(function() {
     $.ajax({
     type: "POST",
     url: $(this).attr("href") + "?format=json", // References "/say/hello?format=json"
     dataType: "json",
     success: function(response) {
     $("h1").html(response.message);
     $("p").html(response.time);
     }
     });
     return false; // keeps the normal request from firing
    });
});})(jQuery);

这三行是我试图合并到我的代码中的内容(因为这就是我认为我需要的):

类型:"开机自检",

url: $(this).attr("href") + "?format=json",//References "/say/hello?format=json"

数据类型:"json",

但是我尝试的一切都没有奏效。我什至无法处理我的实际上传代码来使其工作,因为我甚至无法让上传者启动所需的控制器/操作。

希望有人能够为我指出正确的方向。谢谢!

问题也发布到cfWheels邮件列表:https://groups.google.com/forum/?fromgroups=#!topic/cfwheels/UKk_57y9ncQ

我不

熟悉FineUploader,但通常当我在Wheels中执行AJAX请求时,我会使用urlFor()函数来构建正确的控制器/操作位置。

CFWheels - urlFor

我想我知道你面临什么问题。请按照以下步骤操作:

  1. 确保在控制器中Say.cfc为 Ajax 调用提供 JSON 格式:在 Say.cfcinit()函数中,请确保添加以下内容:

    <cffunction name="init" hint="It secures component from invalid access">
        <!--- this is necessary --->
        <cfset provides("json") />
    </cffunction>
    
  2. 现在,在hello操作中,呈现要提供的数据而不进行布局。删除操作中存在的任何<cfabort>标记:

    <!--- this will go at the very end of the action --->
    <!--- here the attribute data is the data you want to serve: a query, struct or any other custom data generated inside a `<cfsavecontent>`  ---> 
    <cfset renderWith(data=data,layout=false) />
    

我希望这对你有用。