Jquery表单提交和JSON数据类型返回

时间:2021-06-08 16:31:46

I'm using Jquery form plugin to send my form via ajax. The server side php script processes the form data and returns a JSON string in the following format:

我正在使用Jquery表单插件通过ajax发送我的表单。服务器端php脚本处理表单数据并以下列格式返回JSON字符串:

{"error":true,"message":"The username or email already exists. Please try again."}

Here's the ajax to send the form:

这是发送表单的ajax:

$('#register_form').ajaxForm({ 
            dataType: 'json',
            contentType: "application/json; charset=utf-8",
            success:    showResponse

        }); 
});  

Then on my html page, I have the following script:

然后在我的html页面上,我有以下脚本:

<script>
function showResponse(data){

if (data.error == true){
    //display the top error div
    if ($("#registration_error").is(":hidden")) {
        $("#registration_error").html(data.message);
        $("#registration_error").slideDown("slow");
    } 

}
else{
    alert('registration complete');

}


}
</script>

The error is displayed above the registration form in my HTML page. This works in chrome and firefox. However, IE is wiping out the form and displaying a new page with just the JSON reply like so:

错误显示在我的HTML页面中的注册表单上方。这适用于chrome和firefox。但是,IE正在消除表单并显示一个只有JSON回复的新页面,如下所示:

{"error":true,"message":"The username or email already exists. Please try again."}

{“error”:true,“message”:“用户名或电子邮件已存在。请重试。”}

I can't seem to figure out why IE is having problems with this. Please help.

我似乎无法弄清楚为什么IE有这个问题。请帮忙。

1 个解决方案

#1


1  

Try to stop the form from submitting before you're stating the ajaxForm

尝试在陈述ajaxForm之前停止提交表单

$('#register_form').submit(function(ev){ ev.preventDefault(); });

Looks like the jquery was not able to successfully prevent the form from being submitted on IE. Which IE did you use anyways?

看起来jquery无法成功阻止在IE上提交表单。你还使用哪个IE?

#1


1  

Try to stop the form from submitting before you're stating the ajaxForm

尝试在陈述ajaxForm之前停止提交表单

$('#register_form').submit(function(ev){ ev.preventDefault(); });

Looks like the jquery was not able to successfully prevent the form from being submitted on IE. Which IE did you use anyways?

看起来jquery无法成功阻止在IE上提交表单。你还使用哪个IE?