久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

jquery.form 和跨域請求

jquery.form and cross-domain requests(jquery.form 和跨域請求)
本文介紹了jquery.form 和跨域請求的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我很難嘗試使用跨域制作 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:

<form id="contact" action="http://contact.mysite.com/" method="post">

jquery.form 配置有 ajaxForm 調用:

jquery.form is configured with an ajaxForm call:

$(document).ready(function() {

  $('#contact').ajaxForm({
    success: function() { $('#success').fadeIn("slow"); },
    error: function() {  $('#error').fadeIn("slow"); }
  });

});

我遇到的第一個問題是 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):

require 'rubygems'
require 'sinatra'
require 'pony'

# patch sinatra so it handles options requests - see https://stackoverflow.com/questions/4351904/sinatra-options-http-verb
configure do
  class << Sinatra::Base
    def options(path, opts={}, &block)
      route 'OPTIONS', path, opts, &block
    end
  end
  Sinatra::Delegator.delegate :options
end

# respond to options requests so that firefox can do cross-domain ajax requests
options '/' do
  response['Access-Control-Allow-Origin'] = '*'
  response['Access-Control-Allow-Methods'] = 'POST'
  response['Access-Control-Max-Age'] = '2592000'
end

post '/' do
  # use Pony to send an email
  Pony.mail(...)
end

使用 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:

$('#contact').ajaxForm({
  success: function() { $('#success').fadeIn("slow"); },
  error: function(jqXHR, textStatus, errorThrown) {
    console.log(jqXHR.status);
    console.log(jqXHR.statusText);
  }
}); 

jquery 1.4.3 上的輸出(發送 OPTIONS 和 POST 請求后,狀態均為 200):

Output on jquery 1.4.3 (after the OPTIONS & POST requests are sent, both with status 200):

0
(empty string)

jquery 1.5 上的輸出(在 OPTIONS 返回 200 狀態后;從不發送 POST)

Output on jquery 1.5 (after OPTIONS returns with a 200 status; POST is never sent)

302
error

我真的迷路了.

  • 是否有插件可以處理這個問題?
  • 我是否在某處遺漏了什么?

任何幫助將不勝感激.

推薦答案

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:

$('#contact').ajaxForm({
  success: function() { $('#success').fadeIn("slow"); },
  error: function() {  $('#error').fadeIn("slow"); },
  dataType: 'jsonp'
});

上述解決方案依賴于您的服務器以有效的 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:

$('form').submit(function() {
  var url = $(this).attr('action')
  var params = $(this).serialize()
  $.getJSON(url + '?' + params + "&callback=?", function(data) {
    // success
  })
  return false
});

這篇關于jquery.form 和跨域請求的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

jQuery/JavaScript Library for avatar creation?(用于創建頭像的 jQuery/JavaScript 庫?)
How to do following mask input problem?(如何做以下掩碼輸入問題?)
Issues Setting Value/Label Using DropKick Javascript(使用 DropKick Javascript 設置值/標簽的問題)
how to unit-test private methods in jquery plugins?(如何對 jquery 插件中的私有方法進行單元測試?)
stellar.js - configuring offsets / aligning elements for a vertical scrolling website?(stellar.js - 為垂直滾動網站配置偏移量/對齊元素?)
jQuery masked input plugin. select all content when textbox receives focus(jQuery 屏蔽輸入插件.當文本框獲得焦點時選擇所有內容)
主站蜘蛛池模板: 国产精品久久久久久久午夜片 | 国产欧美三区 | 秋霞影院一区二区 | 国产精品69毛片高清亚洲 | 成人av网站在线观看 | 亚洲综合色丁香婷婷六月图片 | 日韩视频三区 | 天堂中文在线播放 | 欧美精品福利 | 成人久久18免费网站图片 | 欧美日韩成人在线 | 国产成人在线观看免费 | 久久亚洲精品国产精品紫薇 | 国产精品日韩一区二区 | 日本三级电影在线免费观看 | 日韩在线免费视频 | 91精品中文字幕一区二区三区 | 免费午夜电影 | 亚洲最新网址 | 成人激情视频在线观看 | 中文字幕一区二区视频 | 久久一区视频 | 亚洲色片网站 | 国产成人久久精品 | 国产这里只有精品 | 亚洲综合婷婷 | 欧美日韩一区二区在线观看 | 99久久久久久99国产精品免 | 男人天堂手机在线视频 | 欧美一区二区久久 | 亚洲成人动漫在线观看 | 国产精品入口 | 日韩三区| 视频一区二区三区中文字幕 | 成年人在线观看 | www.伊人.com | 久久91精品久久久久久9鸭 | 精品国产一区二区三区久久狼黑人 | 欧美一二三四成人免费视频 | 视频国产一区 | 亚洲国产专区 |