2011-04-12 29 views
2

我試圖用jQuery Form Plugi n來處理ajax格式的文件上傳。Chrome jQuery表單插件跨域安全漏洞?

只要我沒有在表單中輸入[type = file],一切都會正常工作。

當我添加一個文件輸入型的形式,將上傳的文件和工作,因爲它應該是在Firefox,但我得到這個錯誤在Chrome:

不安全的JavaScript嘗試訪問 與網址 http://swbdev.net:8888/inc/ajax/edit_page/ 與網址 http://swbdev.net:8888/site-pages-edit/19d8bb79c95e164f736f324d1b09a33e/1/#add_elements。 域名,協議和端口必須匹配 。

它明確指出域,協議和端口必須匹配。我錯過了什麼,在同樣的錯誤,它顯示了兩個URL和域,協議和端口都匹配?

這裏是JavaScript調用插件:

<script type="text/javascript"> 
$(document).ready(function() { 
    var options = { 
     success: function(data) { 
      alert(data); 
     }, 
     dataType: 'html', 
     url: '/inc/ajax/edit_page/' 
    }; 
    $('#add_elements_form').ajaxForm(options); 
}); 
</script> 

更多信息:

它現在沒有在FireFox爲好,不知道爲什麼它前面的工作,但這裏是在Firefox中的錯誤:

權限遭拒, http://swbdev.net:8888獲得 財產Location.href

它指向在插件這一領域的代碼:

  function cb() { 
      if (xhr.aborted) { 
       return; 
      } 
      var doc = io.contentWindow ? io.contentWindow.document : io.contentDocument ? io.contentDocument : io.document; 
      if (!doc || doc.location.href == s.iframeSrc) { 
       // response not received yet 
       if (!timedOut) return; 
      } 
      io.detachEvent ? io.detachEvent('onload', cb) : io.removeEventListener('load', cb, false); 
      var ok = true; 

具體地,該行:

if (!doc || doc.location.href == s.iframeSrc 
+0

將document.domain添加到AJAX所在的.php文件中發佈解決這個問題,但使解釋和解析返回數據更難...... – 2011-04-13 01:30:21

+0

你解決了嗎? – IamDeveloper 2011-06-29 15:42:15

+0

是的。我在AJAX查詢中調用的PHP文件中添加了JavaScript(document.domain)。 – 2011-07-02 18:28:58

回答

0

絕對怪異。因爲輸入[type = file]將要求這樣做,所以我會嘗試將整個事件設置爲「POST」。當然,它應該工作混合,但試試這個。

$(document).ready(function() { 
    var options = { 
     success: function(data) { 
      alert(data); 
     }, 
     dataType: 'html', 
     type: 'POST', // <-- This was added 
     url: '/inc/ajax/edit_page/' 
    }; 
    $('#add_elements_form').ajaxForm(options); 
}); 
+0

不幸的是,這沒有軸承...謝謝你。 – 2011-04-13 00:47:18

2

我最近遇到了與jquery文件上傳相同的問題。錯誤與David B相同

「不安全的JavaScript嘗試訪問來自幀的URL爲http://swbdev.net:8888/inc/ajax/edit_page/的幀,URL爲http://swbdev.net:8888/site-pages-edit/19d8bb79c95e164f736f324d1b09a33e/1/#add_elements。域,協議和端口必須匹配。

在我的情況下,調用頁面url和文件上傳url被指向xxx.mydomain.com,但是當調用頁面被加載時,一個javascript將document.domain設置爲mydomain.com並導致錯誤。在調用頁面加載後檢查document.domain,顯示問題並通過刪除xxx.mydomain.com中的document.domain行來修復。