ajaxfileupload上傳文件出現SyntaxError:unexpected token <錯誤


function ajaxFileUpload() {  
      
  $.ajaxFileUpload  
    (  
      {  
        url: uri,  
        secureuri: false,  
        fileElementId: 'upFile',  
        dataType: 'content', //這里修改為content  
              
        success: function (data, status) {  
          alert(data);  
        },  
                  
        error: function (data, status, e) {  
           alert(e);  
        }  
      }  
    )  
} 

  結果返回的json數據如猜測,json數據被包含在一個<pre></pre>的標簽中.

 

網上查了下原因,是因為Server端的Response上加上了contentType="application/json"。但有時后端這么做是必須的,

所以修改ajaxFileUpload源碼,將<pre></pre>標簽去掉,如下:

uploadHttpData: function( r, type ) {  
        var data = !type;  
        data = type == "xml" || data ? r.responseXML : r.responseText;  
        // If the type is "script", eval it in global context  
        if ( type == "script" )  
            jQuery.globalEval( data );  
        // Get the JavaScript object, if JSON is used.  
        if ( type == "json" ) {  
             ////////////以下為新增代碼///////////////  
             data = r.responseText;  
             var start = data.indexOf(">");  
             if(start != -1) {  
               var end = data.indexOf("<", start + 1);  
               if(end != -1) {  
                 data = data.substring(start + 1, end);  
                }  
             }  
              ///////////以上為新增代碼///////////////  
              eval( "data = " + data);  
        }  
        // evaluate scripts within html  
        if ( type == "html" )  
            jQuery("<div>").html(data).evalScripts();  
  
        return data;  
    } 

  或者是在返回的“content”類型數據后

 

 

得到 JSON 數據


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM