【问题标题】:Coldfusion - Get Variable from another page using AjaxColdfusion - 使用 Ajax 从另一个页面获取变量
【发布时间】:2016-03-02 04:18:13
【问题描述】:

我在通过 Ajax 从另一个 ColdFusion 页面获取变量时遇到问题。我有 2 个文件分别是 uploadForm.cfm 和 act_ajaxAddEditFile.cfm

在uploadForm.cfm 文件中有Ajax 代码。这个 Ajax 将提交表单。此 ajax 还将打开文件 act_ajaxAddEditFile.cfm 文件进行验证。验证完成后,我希望将 act_ajaxAddEditFile.cfm 中的变量 #resultError# 传递给 Ajax。

uploadForm.cfm (Ajax):

$jq('form').submit(function(e) {
  e.preventDefault();
  var type = $jq('#type').val();
  var form = document.getElementById('fileForm');
  var formData = new FormData(form);
  var xhr = new XMLHttpRequest();
  xhr.open('POST', 'pages/subactions/act_ajaxAddEditFile.cfm', true);
  xhr.send(formData);
  xhr.onreadystatechange = function() {
    if (xhr.readyState == 4 && xhr.status == 200) {
      //I want to put variable #resultError# from act_ajaxAddEditFile.cfm here to do IF Statment.       
      var type = $jq('#type').val();
      $jq("#list" + type).load("pages/includes/inc_listFileResult.cfm", {
        type: type,
        qst_id: <cfoutput>#form.qst_id#</cfoutput> 
      }, function(response, status, xhr) {
        if (status == "error") {
          var msg = "Sorry but there was an error: ";
          $jq("#list" + type).html(msg + xhr.status + " " + xhr.statusText);
        }
      });
      $jq("#" + type).empty();
      $jq("." + type).removeClass('hide');
    }
  }
});

ajaxAddEdit.cfm(变量):

<cfif result.status EQ 'OK'>
  <cfoutput>upload FILE</cfoutput>
<cfelse>
  <cfoutput>Fail upload FILE</cfoutput>
  <cfset resultError = #result.error#>
</cfif>

【问题讨论】:

  • 为此,您需要在 cfm 页面中输出变量,而不是 &lt;cfoutput&gt;Fail upload FILE&lt;/cfoutput&gt;&lt;cfset resultError = #result.error#&gt;&lt;cfoutput&gt;#resultError#&lt;/cfoutput&gt;resultError 是哪个数据类型?如果它是一个字符串,你可以做我提到的,或者你可以使用一些编码来显示它。
  • 但是先生,我希望这个变量#resultError# 进入uploadForm.cfm 文件中的Ajax 代码。现在这个变量 #resultError# 在 inc_listFileResult.cfm 文件中。我想知道如何将这个变量#resultError# 从inc_listFileResult.cfm 传递到uploadForm.cfm 文件。谢谢,
  • 可以打印ajaxAddEdit.cfm中的变量,并在xhr.responseText变量中的upload.cfm中获取。
  • 是的,先生!非常感谢你! :)

标签: jquery html ajax variables coldfusion


【解决方案1】:

如果result.error 是字符串,您可以只输出它,否则您可以使用某种编码来显示它。 ajax 调用将接收文本。然后您可以根据需要使用它。

<cfif result.status EQ 'OK'>
  <cfoutput>upload FILE</cfoutput>
<cfelse>
  <cfoutput>#result.error#</cfoutput>
</cfif>

话虽如此,您可以对代码进行一些更改。由于您使用的是 jQuery,因此您可以使用 jQuery ajaxpost 更改基本的 JavaScript ajax 调用。让代码看起来更简单,如下所示。

$jq('form').submit(function(e) {
  e.preventDefault();
  var type = $jq('#type').val();
  $jq.post('pages/subactions/act_ajaxAddEditFile.cfm', $('#fileForm').serialize(), function(resp, status, jqxhr) {
    if (jqxhr.readyState == 4 && jqxhr.status == 200) {
      alert(resp);
      var type = $jq('#type').val();
      $jq("#list" + type).load("pages/includes/inc_listFileResult.cfm", {
        type: type,
        qst_id: '<cfoutput>#form.qst_id#</cfoutput>'
      }, function(response, status, xhr) {
        if (status == "error") {
          var msg = "Sorry but there was an error: ";
          $jq("#list" + type).html(msg + xhr.status + " " + xhr.statusText);
        }
      });
      $jq("#" + type).empty();
      $jq("." + type).removeClass('hide');
    }
  });
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-22
    • 2013-02-13
    • 2018-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多