問題描述
我很難嘗試使用跨域制作 jquery.form要求.我在使用 Firefox 和 Chrome 時遇到問題(甚至還沒有嘗試過 IE).
I'm having a hard time trying to make jquery.form with a cross-domain request. I'm having issues with Firefox and Chrome (didn't even try IE yet).
說明:我的整個網站都位于 http://www.mysite.com 內.但是,我的聯系表單在另一臺服務器上,由 http://contact.mysite.com 引用.我認為將其放在子域上會回避有關跨域請求的問題,但顯然事實并非如此.http://contact.mysite.com 在 Sinatra.
Explanation: my whole site is located inside http://www.mysite.com. However, my contact form is on another server, referenced by http://contact.mysite.com . I thought that putting it on a subdomain would sidestep the issues regarding cross-domain requests, but apparently it didn't. http://contact.mysite.com is implemented in Sinatra.
我的 javascript 設置沒有什么花哨的.表單的action指向http://contact.mysite.com,方法是POST:
My javascript setup is nothing fancy. The form's action points to http://contact.mysite.com and the method is POST:
jquery.form 配置有 ajaxForm 調用:
jquery.form is configured with an ajaxForm call:
我遇到的第一個問題是 Firefox 3.5 - 顯然它發送了一個 OPTIONS 請求,期望服務器提供特定的答案.我使用 this question 來配置我的 Sinatra 應用程序,使其達到預期效果(似乎更多最新版本的 sinatra 包含一個選項動詞):
The first problem I encountered was with Firefox 3.5 - apparently it sends an OPTIONS request expecting an specific answer from the server. I used this question to configure my Sinatra app so it did what was expected (it seems that more recent versions of sinatra include an options verb):
使用 jquery 1.4.3,我在 firebug 上看到一個 OPTIONS 請求,然后是一個 POST 請求(狀態 200.電子郵件已發送).使用 jquery 1.3.2 或 1.5,僅顯示 OPTIONS 請求(未發送電子郵件).
With jquery 1.4.3, I saw on firebug an OPTIONS request followed by a POST request (status 200. The email was sent). With jquery 1.3.2 or 1.5, only the OPTIONS request was shown (the email was not sent).
盡管如此,error
回調總是會在我嘗試過的所有 jquery 版本中觸發.我將其追溯到 $.ajax(...)
調用,所以我不確定這個問題是來自 jquery.form 還是 jquery 本身.
Nevertheless, the error
callback is always fired with all versions of jquery I tried. I traced that down to the $.ajax(...)
call, so I'm not sure of whether this problem comes from jquery.form or jquery itself.
我嘗試注銷來自錯誤的信息:
I tried logging out the information coming from the error:
jquery 1.4.3 上的輸出(發送 OPTIONS 和 POST 請求后,狀態均為 200):
Output on jquery 1.4.3 (after the OPTIONS & POST requests are sent, both with status 200):
jquery 1.5 上的輸出(在 OPTIONS 返回 200 狀態后;從不發送 POST)
Output on jquery 1.5 (after OPTIONS returns with a 200 status; POST is never sent)
我真的迷路了.
- 是否有插件可以處理這個問題?
- 我是否在某處遺漏了什么?
任何幫助將不勝感激.
推薦答案
AJAX 請求無法跨域執行(UPD: 不再正確,所有現代瀏覽器都支持 CORS),但您可以使用 JSONP 代替.雖然 JSONP 可以跨域工作,但它不能用于 POST 請求,您需要將表單的方法更改為 get
并使用:
AJAX requests cannot be executed cross-domain (UPD: not true anymore, all modern browsers support CORS), but you can use JSONP instead. Although JSONP works cross-domain, it can't be used for POST requests, and you'll need to change you form's method to get
and use this:
上述解決方案依賴于您的服務器以有效的 jsonp 響應進行響應,否則將不會執行 success
處理程序.例如:response.write(request.callback + '(' + result.to_json + ')')
The solution above relies on your server responding with a valid jsonp response, otherwise success
handler won't be executed. e.g: response.write(request.callback + '(' + result.to_json + ')')
最新版本的 jQuery 可以在沒有 ajaxForm
插件的情況下序列化表單.如果你不需要文件上傳,你可以使用這個:
Latest versions of jQuery can serialize forms without the ajaxForm
plugin. If you don't need file uploads you can use this:
這篇關于jquery.form 和跨域請求的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!