使用Ajax提交表单是否安全?

Mat*_*ead 4 javascript php ajax

所以我的表单是用html构建的,并在JS中验证,它看起来像我想要的那样.现在显然我将在服务器端使用PHP验证输入,但我想知道它是否足够安全,使用Ajax提交表单然后在服务器端验证,而不是使用'submit'类型提交表单按钮和'动作'属性.基本上,使服务器端验证依赖于JS提交它是否安全?

这是我的表格:

<form name="contactForm" id="contactForm"><!-- The form has no action attribute because its submitted via Ajax -->
<div id="inputsWrapper">
    <div>
        <label for="fullName">Your Name: <span class="required">(required)</span></label>
        <input type="text" name="fullName" id="fullName" title="First &amp; last name" value="First &amp; last name" maxlength="50" />
    </div>
    <div>
        <label for="email">Your E-mail: <span class="required">(required)</span></label>
        <input type="text" name="email" id="email" title="E-mail address" value="E-mail address" maxlength="500" />
    </div>
    <div>
        <label for="subject">In Regards To: <span class="required">(required)</span></label>
        <input type="text" name="subject" id="subject" title="Subject" value="Subject" maxlength="50"/>
    </div>
    <div>
        <label for="message">Your Message: <span class="required">(required)</span></label>
        <textarea name="message" id="message" title="Enter your message here" cols="40" rows="10">Enter your message here</textarea>
    </div>
</div> <!-- End inputsWrapper -->
<input type="button" name="sendBtn" id="sendBtn" value="Send Message" /><!-- This button has a listener assigned to it in JS and submits the form on click -->
Run Code Online (Sandbox Code Playgroud)

单击按钮后,Ajax将通过POST将表单提交到我的PHP脚本,并返回有效或无效状态.这是否是一种安全的方式来做到这一点?谢谢你的任何建议.

Chr*_*son 5

通过Ajax或通过浏览器默认表单发布行为发布它不会从安全角度来看有所不同.请求将是常规HTTP POST/GET请求.


Que*_*tin 5

我想知道使用Ajax提交表单然后在服务器端进行验证是否足够安全,而不是使用"提交"类型按钮和"操作"属性提交表单.

是.系统外部的输入是从系统外部输入的.

基本上,使服务器端验证依赖于JS提交它是否安全?

您的JavaScript应该是不引人注目的并实现渐进增强.

对于脚本的输入应该是相同的,无论它是来自Ajax的常规表单提交(如果你使用像jQuery的序列化这样很容易),所以你不应该让服务器依赖于JS的回应.

表单提交和Ajax请求的处理方式的唯一区别应该是响应的格式.

无论哪种方式,进入系统的数据最终都在提交者的控制之下,因此您需要执行适当的健全性检查并以任何方式转义它.