jQuery ajax交叉域表单提交问题与IE [英] jQuery ajax cross domain form submission issues with IE

查看:95
本文介绍了jQuery ajax交叉域表单提交问题与IE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个IE的问题,大惊喜吧?我有一个网络表单,提交名称,电话和电子邮件到另一个域下的脚本。我是jquery.ajax提交表单。我也使用jquery.validate表单验证。姓名和电子邮件地址。手机是可选的。

I'm having a problem with IE, big surprise right? I have a webform that submits name, phone, and email to a script that under another domain. I'm jquery.ajax to submit the form. I'm also using jquery.validate for form validation. name and email are required. phone is optional.

当我在Chrome中提交表单时,它完美地工作。我收到我的谢谢电子邮件,我的数据到达其他域上的脚本。

When I submit the form in Chrome, it works perfectly. I receive my thank you email and my data reaches the script on the other domain.

当我在IE9尝试相同的形式,我填写所有字段,它看起来像它的提交(我收到感谢消息,其中的形式是),但没有数据使它的服务,我没有得到一个确认电子邮件 - 所以它不实际发布。

When i try the same form in IE9, I fill in all fields and hit submit and it looks like its submitted (I receive the thank you message where the form is), but none of the data makes it to the service and I don't get a confirmation email either - so its not actually posting.

那么我该如何得到这个脚本在IE7,8,9工作。这里是脚本,因为它现在...当我取消注释成功操作的警报,我得到在IE的警报。它只是不发布数据。我在这里看到了很多关于这个的帖子,但我没有把所有的片段以一种理解的方式在一起。

So how can i get this script to work in IE7,8,9. Here's the script as it right now...When I uncomment the alert in the success action, I get the alert in IE. It's just not posting the data. I've seen alot of posts on here about this, but I'm not getting all of the pieces together here in an understandle fashion.

jQuery.validator.setDefaults({
submitHandler: function() {

var cons_info_component = jQuery("#cons_info_component").val();
var cons_mail_opt_in = jQuery("#cons_mail_opt_in").val();
var longnum = jQuery("#1540_6383_2_8347_1").val();
var survey_id = jQuery("#SURVEY_ID").val();
var cons_email_opt_in = jQuery("#cons_email_opt_in").val();
var cons_email_opt_in_requested = jQuery("#cons_email_opt_in_requested").val();
var cons_first_name = jQuery("input#cons_first_name").val();
var cons_last_name = jQuery("input#cons_last_name").val();
var cons_email = jQuery("input#cons_email").val();
var cons_phone = jQuery("input#cons_phone").val();

if(cons_phone === "PHONE NUMBER (OPTIONAL)") { cons_phone = ''; }

var ACTION_SUBMIT_SURVEY_RESPONSE = jQuery("#ACTION_SUBMIT_SURVEY_RESPONSE").val();

var dataString = 'cons_info_component='+cons_info_component+'&cons_mail_opt_in='+cons_mail_opt_in+'&1540_6383_2_8347='+longnum+'&SURVEY_ID='+survey_id+'&cons_first_name='+cons_first_name+'&cons_last_name='+cons_last_name+'&cons_email='+cons_email+'&cons_phone='+cons_phone+'&cons_email_opt_in='+cons_email_opt_in+'&cons_email_opt_in_requested='+cons_email_opt_in_requested+'&ACTION_SUBMIT_SURVEY_RESPONSE='+ACTION_SUBMIT_SURVEY_RESPONSE;


jQuery.ajax({   
type: "POST",
url: "http://urlthatIsubmittoo.com/script/",
crossDomain: true,
cache: false,
data: dataString,

statusCode: {
404: function() {
  //alert('page not found');
},
200: function() {
  //alert('success');
}
},
 success: function() {
    //alert('submited');    
},
 complete: function() {

    jQuery('div.convioform').html("<div class='tybox'></div>"); 
    jQuery('div.convioform .tybox').html("<h5 class='webform-title'>Thank You</h5><p>You'll soon be receiving our e-mails and updates now.</p>");
        }
    }); // close .ajax line

},  
 });


推荐答案

将此文件插入文件顶部/ jaubourg):

Insert this at the top of your file (via github/jaubourg):

if ( window.XDomainRequest ) {
    jQuery.ajaxTransport(function( s ) {
        if ( s.crossDomain && s.async ) {
            if ( s.timeout ) {
                s.xdrTimeout = s.timeout;
                delete s.timeout;
            }
            var xdr;
            return {
                send: function( _, complete ) {
                    function callback( status, statusText, responses, responseHeaders ) {
                        xdr.onload = xdr.onerror = xdr.ontimeout = jQuery.noop;
                        xdr = undefined;
                        complete( status, statusText, responses, responseHeaders );
                    }
                    xdr = new XDomainRequest();
                    xdr.onload = function() {
                        callback( 200, "OK", { text: xdr.responseText }, "Content-Type: " + xdr.contentType );
                    };
                    xdr.onerror = function() {
                        callback( 404, "Not Found" );
                    };
                    xdr.onprogress = jQuery.noop;
                    xdr.ontimeout = function() {
                        callback( 0, "timeout" );
                    };
                    xdr.timeout = s.xdrTimeout || Number.MAX_VALUE;
                    xdr.open( s.type, s.url );
                    xdr.send( ( s.hasContent && s.data ) || null );
                },
                abort: function() {
                    if ( xdr ) {
                        xdr.onerror = jQuery.noop;
                        xdr.abort();
                    }
                }
            };
        }
    });
}

这篇关于jQuery ajax交叉域表单提交问题与IE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆