ColdFusion的CFC CORS和AJAX的帖子 [英] ColdFusion CFC CORS and AJAX posts

查看:116
本文介绍了ColdFusion的CFC CORS和AJAX的帖子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让发送到远程服务器的形式。总的想法,现在,是该HTML将在本地运行,并会通过AJAX上传到远程服务器。

I'm trying to get a form posted to a remote server. The general idea, for now, is that the HTML will run locally and will post to a remote server via AJAX.

所以这是一个形式,在JS和CFC其调职。

So there's a form, the JS and the CFC it's posting to.

下面是JS

$(document).ready(function () {
$("#submit").click(function(){
    var setName = $("input[name='setName']").val();
    var setNumber = $("input[name='setNumber']").val();
    var setTheme = $("input[name='setTheme']").val();

    var retailPrice = $("input[name='retailPrice']").val();
    var purchaseStore = $("input[name='purchaseStore']").val();
    var purchaseDate = $("input[name='purchaseDate']").val();
    var purchasePrice = $("input[name='purchasePrice']").val();

    var condition = $("input[name='condition']").val();

    var sellPrice = $("input[name='sellPrice']").val();
    var sellStore = $("input[name='sellStore']").val();
    var selldate = $("input[name='selldate']").val();

$.ajax({
    type: 'get',
    url: 'http://www.chesteraustin.us/cfc/entry.cfc?ReturnFormat=json',  
    data: {
        method: 'setEntry',
        Set_Name: setName, //CFARGUMENT: JS_VARIABLE
        Set_Number: setNumber,
        Set_Theme: setTheme,
        Retail_Price: retailPrice,
        Purchase_From: purchaseStore,
        Purchase_Price: purchasePrice,
        Purchase_Date: purchaseDate,
        Status: condition,
        Sell_Date: sellPrice,
        Sell_from: sellStore,
        Sell_date: selldate
        },
    contentType: 'json',
    dataType: 'json',
    success: function(response) {
        console.log("you da man");
        }
    });    
}); 
});

下面是CFC,它被张贴到(我剪了很多的它简洁):

Below is the CFC that it is being posted to (I've cut out a lot of it for brevity):

<cfcomponent>
<cfheader name="Access-Control-Allow-Origin" value="*" />
<cfheader name="Access-Control-Allow-Methods" value="GET,PUT,POST,DELETE" />
<cfheader name="Access-Control-Allow-Headers" value="Content-Type" />

<cffunction name="setEntry" access="remote">
    <cfreturn 1>
</cffunction>

</cfcomponent>

编辑:整理了CFC,去掉多余的注释

Cleaned up CFC, removed extraneous comments.

做研究,我遇到了 CFHEADER 本来是要顶上去,让交叉血统,但是,Chrome浏览器仍然presents一个否访问控制 - 允许 - 原产地标头的请求的资源present。错误。

Doing research, I've come across that CFHEADER was supposed to go on top, to allow cross origin, however, Chrome still presents a No 'Access-Control-Allow-Origin' header is present on the requested resource. error.

一对夫妇的背景件事情:我是一个共享的主机上。我的CFC所在的文件夹中的空白的Application.cfc。

A couple background things: I'm on a shared host. I have a blank Application.CFC in the folder that the CFC resides in.

推荐答案

这么多的研究之后,我已经想通了解决方案。该ColdFusion的code按预期工作。然而,还有别的控制标头(在此情况下,它是阿帕奇)。

So after much research, I've figured out the solution. The ColdFusion code works as intended. However, there was something else controlling the headers (in this case, it was Apache).

使用 http://enable-cors.org/server_apache.html 为指导,我修改我的.htaccess文件我用下面的public_html目录:标题设置访问控制 - 允许 - 起源*。铬想出了另一个错误,指出访问控制 - 允许 - 标题没有内容类型,所以我补充说,还有:标题设置访问控制,允许报头的Content-Type 。你瞧,这得到它的工作。

Using http://enable-cors.org/server_apache.html as a guide, I modified my .htaccess file in my public_html directory with the following: Header set Access-Control-Allow-Origin "*". Chrome came up with another error, stating that Access-Control-Allow-Headers didn't have Content-Type, so I added that in as well: Header set Access-Control-Allow-Headers Content-Type. Lo and behold, that got it working.

总之,如果在ColdFusion的CORS不工作:&LT; cfheader名=访问控制 - 允许 - 起源值=*/&GT; ,请检查Web服务器的配置。我的的.htaccess 文件现在有两行,CORS是现在的工作。

In summary, if CORS on ColdFusion isn't working: <cfheader name="Access-Control-Allow-Origin" value="*" />, check the web server configuration. My .htaccess file now has two lines, and CORS is working now.

Header set Access-Control-Allow-Origin "*"
Header set Access-Control-Allow-Headers Content-Type

这篇关于ColdFusion的CFC CORS和AJAX的帖子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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