2016-10-14 103 views
2

我想知道我是否可以使用ajax和.cfc页面上的coldfusion函数结合上传文件?我已经在JQuery中创建了我的ajax调用,并且我在.cfc页面上有功能,但是在调用之后我收到了奇怪的ajax响应。这里是我的代码:如何在ColdFusion .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页:即在alert(obj)输出

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

我的响应消息是这样的:

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

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

+0

也许你应该在使用的ColdFusion serializeJSON()然后检查序列返回JSON格式值条件。 –

回答

0

你不必“检查”的状态200,如.done()只有成功火灾,即状态码200

+0

所以你说的是,如果我可以访问.done()中的alert(),这意味着我的调用成功了吗? –

+0

是的。但是,这取决于您使用的版本。您可能需要使用'.success()'来代替。 http://api.jquery.com/jquery.ajax/ – Jules

相关问题