简体   繁体   English

ColdFusion CFC CORS和AJAX帖子

[英]ColdFusion CFC CORS and AJAX posts

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. 到目前为止,一般的想法是HTML将在本地运行并通过AJAX发布到远程服务器。

So there's a form, the JS and the CFC it's posting to. 这样便有一个表单,即JS和CFC。

Below is the JS 下面是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");
        }
    });    
}); 
});

Below is the CFC that it is being posted to (I've cut out a lot of it for brevity): 以下是要发布到的CFC(为简洁起见,我将其剪切了很多):

<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>

EDIT: Cleaned up CFC, removed extraneous comments. 编辑:清理CFC,删除了多余的注释。

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. 做研究时,我发现应该将CFHEADER ,以允许跨源访问,但是,Chrome仍然会No 'Access-Control-Allow-Origin' header is present on the requested resource.上显示CFHEADER No 'Access-Control-Allow-Origin' header is present on the requested resource. error. 错误。

A couple background things: I'm on a shared host. 一些背景知识:我在共享主机上。 I have a blank Application.CFC in the folder that the CFC resides in. 我在CFC所在的文件夹中有一个空白的Application.CFC。

So after much research, I've figured out the solution. 因此,经过大量研究,我找到了解决方案。 The ColdFusion code works as intended. ColdFusion代码按预期工作。 However, there was something else controlling the headers (in this case, it was Apache). 但是,还有其他东西控制标头(在本例中为Apache)。

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 "*" . 使用http://enable-cors.org/server_apache.html作为指南,我使用以下内容修改了public_html目录中的.htaccess文件: 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 . Chrome出现了另一个错误,指出Access-Control-Allow-Headers没有Content-Type,因此我也添加了它: Header set Access-Control-Allow-Headers Content-Type Lo and behold, that got it working. 瞧,这使它起作用了。

In summary, if CORS on ColdFusion isn't working: <cfheader name="Access-Control-Allow-Origin" value="*" /> , check the web server configuration. 总之,如果ColdFusion上的CORS无法正常工作: <cfheader name="Access-Control-Allow-Origin" value="*" /> ,请检查Web服务器配置。 My .htaccess file now has two lines, and CORS is working now. 我的.htaccess文件现在有两行,并且CORS现在正在工作。

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

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

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