ajaxfileupload上傳文件出現SyntaxError:unexpected token

修改dataType的值,

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章