【问题标题】:How to upload file with the ajax call in ColdFusion .cfc function?如何在 ColdFusion .cfc 函数中使用 ajax 调用上传文件?
【发布时间】:2017-02-24 04:13:21
【问题描述】:

我想知道我是否可以在.cfc 页面上使用ajax 结合coldfusion 功能上传文件?我已经在 J​​Query 中创建了我的 ajax 调用,并且在 .cfc 页面上有功能,但是在调用之后我得到了奇怪的 ajax 响应。这是我的代码:

<form name="myForm" id="myForm" method="POST">
  <table role="presentation">
    <tr>
        <td>Order:
            <select name="order" id="order">
               <option value="1">1</option>
               <option value="2">2</option>
               <option value="3">3</option>    
            </select>
        </td>
    </tr>
    <tr>
        <td>User ID: 
            <select id="userID" name='userID'>
               <option value='0567'>0567</option>
               <option value='0568'>0568</option>
               <option value='0569'>0569</option> 
            </select>
        </td>
    </tr>
    <tr>
       <td>File for upload:   
          <input type="file" id="fileUpload" name="fileUpload" onChange="fileSubmit()"/>
       </td>
    </tr>
  </table>
</form>

Ajax 调用:

function fileSubmit(){
   var myForm = new FormData(document.getElementById('myForm'));

   $.ajax({
      type: 'POST',
      url: 'FileUpload.cfc?method=fileUpload',
      data: new FormData($('#myForm')[0]),
      async: false,
      cache: false,
      contentType: false,
      enctype: 'multipart/form-data',
      processData: false,
   }).done(function(obj){
      //I want to check for the status here
      // something like this 
      //if(obj.STATUS === 200){
      alert(obj);
   }).fail(function(jqXHR, textStatus, errorThrown){ 
      alert(errorThrown);
   })
}

FileUpload.cfc 页面:

<cfcomponent>
    <cfsetting enablecfoutputonly="yes" showdebugoutput="no">
    <cffunction name="fileUpload" access="remote" output="true">
        <cfargument name="order" type="string" required="yes">
        <cfargument name="userID" type="string" required="yes">
        <cfargument name="fileUpload" type="string" required="yes">

        <cfset fncResults = StructNew()>

        <cfif arguments.order NEQ '' and arguments.userID NEQ ''>
            //Here is my file upload and I'm setting status of 200
            <cfset fncResults.status = "200">
        <cfelse>
            <cfset fncResults.status = "400">
            <cfset fncResults.message = "Invalid access attempt">
        </cfif>

        <cfreturn fncResults>
    </cffunction>
</cfcomponent>

alert(obj) 中输出的我的响应消息如下所示:

<wddxPacket version='1.0'><header/><data><struct><var name='STATUS'><string>200</string></var></struct></data></wddxPacket>

我想知道在这种情况下如何检查 200 的状态?通常我过去返回到页面的所有内容都是 JSON 格式。如果有人可以帮助解决这个问题,请告诉我。

【问题讨论】:

  • 也许你应该在coldfusion中使用serializeJSON()序列化返回的JSON格式值,然后检查条件。

标签: javascript jquery ajax file-upload coldfusion


【解决方案1】:

您不必“检查”状态 200,因为 .done() 仅在成功时触发,即状态代码 200。

【讨论】:

  • 那么你的意思是如果我可以访问 .done() 中的 alert() 是否意味着我的调用成功了?
  • 是的。但是,这取决于您使用的版本。您可能需要改用.success()。 api.jquery.com/jquery.ajax
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多