从ColdFusion的CFC返回值被称为使用jQuery阿贾克斯 [英] Returning value from Coldfusion CFC called with JQuery Ajax

查看:107
本文介绍了从ColdFusion的CFC返回值被称为使用jQuery阿贾克斯的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图调用CFC组件使用Ajax。但我需要通过商品共同基金,以我的javascript程序返回的值。该完成函数创建一个警报,当我显示正确的消息信息。但 RET 变量从未被填充了值。我需要回到更改JavaScript数据流量的价值。

我是什么做错了吗?

  VAR RET = $阿贾克斯({
              类型:后,
              网址:CFC / CFCSemae.cfc
              数据: {
                  方法:verificaCadastro
                  returnformat:普通,
                  queryformat:列,
                  nro_cadastro:num_cad
      },
      数据类型:HTML,
      })做(函数(MSG){返回味精;});

警报(RET);
 

解决方案

以下code然而应该为你做,明白,当你正在做的异步请求,只将执行回调时,你得到的结果。您的code可以简单的叫你 $。阿贾克斯的功能,但它不会为响应等待(因为如果它是在不同的线程)。当响应准备就绪后,回调完整执行。如果响应返回成功消息(HTTP状态200),在成功回调被调用时,错误被称为除外。

  //在脚本中的某处
VAR RET ='';

//然后在Ajax请求
$阿贾克斯({
    类型:后,
    网址:CFC / CFCSemae.cfc
    数据:{方法:verificaCadastro,returnformat:普通,
            queryformat:列,nro_cadastro:num_cad},
    数据类型:JSON,
    的contentType:应用/ JSON的;字符集= UTF-8,//确保你的服务器端函数可以返回JSON
    成功:功能(数据){
        //自己的验证检查数据是否是一个有效响应
        RET =数据;
    },
    错误:函数(XHR,味精){
        //处理错误
    }
});
 

请阅读下面的文章关于Ajax编程。这会给你一个更好地了解异步编程是: http://en.wikipedia.org/维基/ Ajax_(编程

修改的:纠正拼写错误

I'm trying to call an CFC component with Ajax. But I need to pass the value returned by the CFC to my javascript routine. The done function creates the correct message when I display msg in an alert. But the ret variable is never populated with that value. I need the value returned to alter the javascript data flow.

What am I doing wrong?

var ret = $.ajax({
              type: "post",
              url: "cfc/CFCSemae.cfc",
              data: {
                  method: "verificaCadastro",
                  returnformat: "plain",
                  queryformat: "column",
                  nro_cadastro: num_cad
      },
      dataType: "html",
      }).done(function(msg){ return msg; });

alert(ret);

解决方案

The following code should do it for you, however understand that when you are doing async requests, you'll only get the result when the callback is executed. Your code will simply call you $.ajax function, but it will not wait for a response (as it if it's in a different thread). When the response is ready, the callback complete is executed. If the response return a success message (HTTP Status 200), the success callback is called, error is called otherwise.

//somewhere in your script
var ret = '';

// then at the ajax request
$.ajax({
    type: "post",
    url: "cfc/CFCSemae.cfc",
    data: { method: "verificaCadastro", returnformat: "plain",
            queryformat: "column", nro_cadastro: num_cad },
    dataType: "json",
    contentType: "application/json; charset=utf-8", // make sure your serverside function can return json
    success: function(data) {
        //your own validation to check whether 'data' is a valid response 
        ret = data;
    },
    error: function(xhr, msg) {
        // handle error
    }
});

Please read the following article about ajax programming. This will give you a better understanding of what asynchronous programming is: http://en.wikipedia.org/wiki/Ajax_(programming)

Edit: corrected misspelling

这篇关于从ColdFusion的CFC返回值被称为使用jQuery阿贾克斯的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆