简体   繁体   English

jQuery ajax发布在IE,FF,Chrome中正常运行,但在Safari中无法正常运行

[英]jquery ajax post works fine in IE, FF, Chrome but not Safari

I am posting a form to a remote server via the action method, but calling a jquery function to updata a database prior to the post - all works fine in IE, FF etc. ... but, Safari always fails to do the jquery database insert when return is true OR async: true, ... if both are false, (so the html form does not manage to post due to the false declaration) it works perfectly? 我正在通过action方法将表单发布到远程服务器,但是在发布之前调用jquery函数来更新数据库-在IE,FF等中一切正常,但是,Safari始终无法执行jquery数据库当return为true或async:true,...时插入,如果两者均为false,(那么由于错误的声明,html表单将无法发布),是否可以正常工作? But when true, the form will just POST, but the jquery ajax post will not run? 但是当为true时,表单将仅POST,但是jquery ajax帖子将不会运行?

The Form ... 表格 ...

 <form action="http://anotherplace" method="post" id="myform" >

<button class="button-big" type="submit" id="myform-post" ><span>Continue</span></button>

The function ... 功能 ...

   $(document).ready(function() { //available when doc renders


   $("form#myform").submit(function(){

    var title = $("select#title").val();
    if (title == "") {
    $("select#title").css({backgroundColor:"#ccc"});        
    $("select#title").focus();
     return false;
   }

     var firstname = $("input#fname").val();
    if (firstname == "") {
     $("input#fname").css({backgroundColor:"#ccc"});
     $("input#fname").focus();
     return false;    }

      var surname = $("input#sname").val();
    if (surname == "") {
     $("input#sname").css({backgroundColor:"#ccc"});
     $("input#sname").focus();
     return false;    }



//Grab the form values to pass via AJAX for DB insert

var cameraMake = $("input#cameraMake").val();
var cameraModel = $("input#cameraModel").val();
var cameraValue = $("input#cameraValue").val();
var mp3Make = $("input#mp3Make").val();
var mp3Model = $("input#mp3Model").val();
var mp3Value = $("input#mp3Value").val();


        var dataString='&title='+ title  + '&firstname=' + firstname + '&surname=' + surname + '&add1=' + add1 + '&add2=' + add2 + '&add3=' + add3 + '&pcode=' + pcode + '&email=' + email + '&phone=' + phone +'&username=' + username + '&pword=' + pword; 

var ajaxComplete = false;
if( !ajaxComplete ){
        $.ajax({
            async: true,              
            type: "POST",
            url: "process-.php",
            data: dataString,
            success: function() {
               // $('#details').html("<div id='post-message'></div>");
               // $('#post-message').html("<h2 style='color:#fff'>Thanks for getting in touch.</h2>")

                ajaxComplete = true;
                this.submit();

            }
        });
        return false; // Abort form submission by now
    }else{
        return true; // Submit normally
    }
});


}); //End of document.ready

The script runs on an 'click' method based on the submit ID (does validation then the AJAX post). 该脚本基于提交ID在“点击”方法上运行(先进行验证,然后进行AJAX发布)。

Can ayone offer any advice on whe this fails in Safari (all versions tested) or offer a better solution to do what I need? 如果在Safari(测试所有版本)中均失败,yone可以提供任何建议还是提供更好的解决方案来满足我的需要? - something to do with async / posting? -与异步/发布有关? As I say, set to false and it works fine ... BUT, I need the HTML form to still post! 就像我说的那样,将其设置为false可以正常工作...但是,我仍然需要HTML表单才能发布!

Many Thanks! 非常感谢!

If I understand correctly, you want to unload the page while there's a pending AJAX request. 如果我理解正确,那么您想在有待处理的AJAX请求时卸载该页面。 That has little chance to work correctly, even in the browsers where it appears to work. 即使在看起来可以正常工作的浏览器中,也几乎没有机会正常工作。

My advice is to initially abort the form's POST request ( return false; ) and add code to the success handler to trigger the form submission when the AJAX request completes. 我的建议是首先中止表单的POST请求( return false; ),并将代码添加到success处理程序中,以在AJAX请求完成时触发表单提交。 You can use a flag variable so the AJAX request only triggers the first time. 您可以使用标志变量,以便AJAX请求仅在第一次触发。

Update: 更新:

Some quick, dirty and untested code. 一些快速,肮脏且未经测试的代码。 I've also realised you've omitted the start of the code block so I've simply invented it: 我还意识到您已经省略了代码块的开头,因此我只是发明了它:

var ajaxComplete = false;
$("form#3-form").submit(function(){
    if( !ajaxComplete ){
        $.ajax({
            async: true,              
            type: "POST",
            url: "process-.php",
            data: dataString,
            success: function() {
                $('#details').html("<div id='post-message'></div>");
                $('#post-message').html("<h2 style='color:#fff'>Thanks for getting in touch.</h2>")

                ajaxComplete = true;
                this.submit();

            }
        });
        return false; // Abort form submission by now
    }else{
        return true; // Submit normally
    }
});

you should use 你应该使用

return false;

and then go with form action 然后采取形式行动

window.location.url = "YOUR URL";

So 所以

$.ajax({
    async: true,              
    type: "POST",
    url: "process-.php",
    data: dataString,
    success: function() {
        $('#details').html("<div id='post-message'></div>");
        $('#post-message').html("<h2 style='color:#fff'>Thanks for getting in touch.</h2>");
        window.location.url = "YOUR URL";
    }
});
return false;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM