简体   繁体   English

ColdFusion中的AJAX请求

[英]AJAX request in ColdFusion

How can I do a AJAX request in ColdFusion? 如何在ColdFusion中提出AJAX请求?

I have my javascript: 我有我的JavaScript:

function getdata(){
    var formElements=document.getElementById("CFForm_1").elements;    
    var data=[];
    for (var i=0; i<formElements.length; i++){
        if(formElements[i].name == 'customersid')
            data.push({'customersid':document.getElementById("customersid").value});
        if(formElements[i].name == 'customerstoid')
            data.push({'customerstoid':document.getElementById("customerstoid").value});
    }

    $.ajax(
    {
        type: "get",
        url: "components/BillingCalc.cfc",
        data: {
                method:"ajaxGetTotalCost",
                data: data.join()
            },
        dataType: "json",
        success: function( objResponse ){

        }
    });
  }

My component: 我的组件:

component displayName="Calc" {

remote function ajaxGetTotalCost(data){
    data = deserializeJSON(arguments.data);
    WriteDump(data); abort;
}

I am getting the error: JSON parsing failure at character 2:'o' in [object Object],[object Object] Does anyone knows how to do AJAX request in CF? 我收到错误消息:[对象对象],[对象对象]中字符2:'o'处的JSON解析失败有人知道如何在CF中执行AJAX请求吗?

This function: 该功能:

remote function ajaxGetTotalCost(data){
data = deserializeJSON(arguments.data);
WriteDump(data); abort;
}

is not complete. 不完整。 It's at the stage where you have to call it from a ColdFusion page, not with javascript. 在这个阶段,您必须从ColdFusion页面调用它,而不是使用javascript。 That will enable you to see the results of the writedump(data) command to ensure it's what you expect. 这将使您能够查看writedump(data)命令的结果以确保它符合您的期望。 You have to add more code to the function to get it to produce a variable javascript can receive, and then return that variable to whatever is calling the function. 您必须向该函数添加更多代码,以使其产生javascript可接收的变量,然后将该变量返回给调用该函数的对象。

The issue is related to dataType attribute you are passing with $.ajax() method. 问题与您通过$.ajax()方法传递的dataType属性有关。 dataType: "json" indicates your AJAX request is expecting JSON data as a response. dataType: "json"表示您的AJAX请求需要JSON数据作为响应。 But in your case you are simply returning DUMP of the deserialized JSON, which is HTML not JSON . 但是在您的情况下,您只是返回反序列化JSON的DUMP ,这是HTML而不是JSON So if you want it to work properly, then you need to return JSON data from your ColdFusion function. 因此,如果您希望它正常运行,则需要从ColdFusion函数返回JSON数据。 You can try this and see if it works. 您可以尝试一下,看看是否可行。

remote function ajaxGetTotalCost(data){
   data = deserializeJSON(arguments.data);
   return serializeJSON(data));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM