jQuery   发布时间:2022-03-30  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了使用jQuery AJAX将JSON发布到CFC大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试使用带有CFC的 Jquery / AJAX / JSON来发送电子邮件(这很简单吗?)花了很多时间试图找到一个完整的例子来说明这应该如何工作,我已经接近但是我坚持到最后部分.我的失败很明显.

我收到“不支持的操作错误.请检查应用程序日志以获取更多详细信息.”从(我认为)ColdFusion回来.

生成该消息的GET请求来自以下URL:

http://www.dumbass.com/CFIDE/componentutils/cfcexplorer.cfc?method=getcfcinhtml&name=blah.www.test&path=/blah/www/test.cfc

我可以在浏览器中调用我的CFC,它会发送一个基本的测试电子邮件

http://servername/test.cfc?method=sendMail

CFC的表格邮寄方式如下:

@H_179_11@method=sendMail&name=BOB&email=bob%40bitchin.com&subject=message+from+contact+form&message=bob+is+really+bitchin

这是jQuery

$.ajax({
    type: "POST",url: "test.cfc?method=sendMail",data: {
        "name": $("#footer-form #name2").val(),"email": $("#footer-form #email2").val(),"subject": "message from contact form","message": $("#footer-form #message2").val()
    },dataType: "json",success: function(data) {
        if (data.sent == "yes") {
            $("#messageSent2").removeClass("hidden");
            $("#messageNotSent2").addClass("hidden");
            $(".submit-button").removeClass("btn-default").addClass("btn-success").prop('value','message Sent');
            $("#footer-form .form-control").each(function() {
                $(this).prop('value','').parent().removeClass("has-success").removeClass("has-error");
            });
        } else {
            $("#messageNotSent2").removeClass("hidden");
            $("#messageSent2").addClass("hidden");
        }
    }
});

这是CFC,(甚至没有尝试使用提交的表单字段):

<cfcomponent output="false" access="remote">
   <cffunction name="sendMail" access="remote" returntype="void">
     <cfmail from="do-not-reply@dumbass.com" 
      to="illiquent@gmail.com" subject="duh!!!">
      You got mail!
     </cfmail>
  </cffunction>

AJAX将恢复为失败消息创建的HTML:“不支持的操作.检查应用程序日志以获取更多详细信息.”.

我在共享的CF机器上,无法访问应用程序日志.它是否将数据作为JSON传递,这让我感到悲伤?我应该在表单上使用serialize()吗?

在AJAX Jquery中,我有test.cfc?method = sendMail.我应该将该方法放在AJAX调用的’data’块中吗?

我已经安装了小提琴手,并且正在查看标题,但我仍然不知道为什么通过Jquery和AJAX向CFC提交数据应该是一个多么神秘的事情.

如果有人能够理顺我或指出一个有效的例子,我将非常感激.

@R_197_9531@E

在查看我的javascript之后 – 我看到我有一个不同的功能也被表单提交调用 – 这导致“检查应用程序日志以获取更多详细信息”.以下是导致该问题的功能

$(document).ready(function () {
            //this function was getTing called in addition to one posted     
            // above. 
            $("input#submit1").click(function(){
                console.log('WTF');
                console.log($('form#footer-form').serialize());
                $.ajax({
                    type: "POST",// this is where I wasn't specifying the method!
                    // it should have been test.cfc?method=sendMail
                    url: "test.cfc",//process to mail
                    // still thinking about serialize to make this flexible
                    data: $('form#footer-form').serialize(),success: function(msg){
                        $("#thanks").html(msg) //hide button and show thank you
                        $("#form-content").modal('hide'); //hide popup  
                    },error: function(){
                        alert("failure");
                    }
                });
            });
        });

这是我更新的CFC“工作”:

<cffunction name="sendMail" access="remote" returnformat="json" returntype="any">
   <cfargument name="name" required="true" />     
   <cfargument name="subject" required="true" />
   <cfargument name="email" required="true" />
   <cfargument name="message" required="true" /> 



      <cfmail from="do-not-reply@dumbass.com" 
       to="me@dumbass.com"   subject="#subject#">
      Owning it!
     #name# #subject# #email# #message#
      </cfmail>

      <cfset result ='{"sent":"yes"}'> 

    <cfreturn result />

   </cffunction>

我正在努力的最后一点是如何正确地允许使用returntype =“json”以及如何创建返回值.
我有返回类型=“任何”因为我在返回类型=“json”时遇到错误.我想用returntype =“json”.不过这个:

<cfset result ='{"sent":"yes"}'> 
<cfreturn result />

抛出错误.创建类似结构的正确方法是什么,@R_381_9447@使用returntype =“json”?我相信将来我会想要从CFC返回JSON,并且不想手动构建字符串……如果这有意义的话.

<cfif success>
  <cfset result ='{"sent":"yes"}'> 
<cfelse>
   <cfset result ='{"sent":"no"}'>
</cfif>

如何制作“结果”以便我可以使用returntype =“json”?

解决方法

而不是test.cfc使用test.cfm页面.这是它的代码

<cfswitch expression="#url.method#">

<cfcase value="sendMail">
     <cfmail from="do-not-reply@dumbass.com" 
      to="illiquent@gmail.com" subject="duh!!!">
      You got mail!
     </cfmail>

    <cfset result = structNew()>
    <cfset result['sent'] = true />
    <cfoutput>#serializeJSON(result)#</cfoutput>

</cfcase>

</cfswitch>

你还需要调整一下javascript

电话应该是

url: "test.cfm?method=sendMail",

而不是

if (data.sent == "yes") {

你需要

if (data.sent) {

大佬总结

以上是大佬教程为你收集整理的使用jQuery AJAX将JSON发布到CFC全部内容,希望文章能够帮你解决使用jQuery AJAX将JSON发布到CFC所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。