2
我正在使用Symfony2開發一個簡單的博客應用程序。
我想通過AJAX來檢查密碼數據:Symfony2:JQuery請求不是XHR
- 如果數據是正確的 - >前進到另一個頁面
- 如果不是 - >用400個響應狀態和動畫形式
我的問題是,請求,JQuery發送,似乎不是XHR。
HTML:
<form id="logfo" action="{{path('Login_Ajax')}}" method="POST">
<p> Name </p>
<input type="text" id="logname"> </br>
<p> Password </p>
<input type="text" id="logpass"> </br>
<input type="submit" value="Log In">
</form>
使用Javascript:
$(document).ready(function() {
//listen for the form beeing submitted
$("#logfo").submit(function(){
var url = $("#logfo").attr("action");
//start the post
$.post(url,{Name:$("#logname").val(),
Password:$("#logpass").val()},
function(data){
if(data.responseCode==400) {
//animate the Form
var fo=$("#logfo");
fo.animate({left:'+=25px'}, "fast");
fo.animate({left:'-=50px'}, "fast");
fo.animate({left:'+=50px'}, "fast");
fo.animate({left:'-=25px'}, "fast");
}
});
});
});
的routing.yml:
Login_Ajax:
pattern: /thefitthing/logjax
defaults: {_controller: FitcompTheFitThingBlogBundle:Ajax:login }
requirements:
_method: POST
對於測試的原因,我取代代碼,用於比較數據
請求來自具有簡單的if-terms的數據庫。
控制器:
class AjaxController extends Controller {
public function logInAction()
{
$request = $this->getRequest();
if ($request->isXmlHttpRequest()) {
$name=$request->request->get('Name');
if($name == "page") return new Response("",400);
if($name == "plant") return $this->redirect($this->generateUrl('Thanks_Page'));
}
else return $this->render('FitcompTheFitThingBlogBundle:Blog:test.html.twig', array('hint' => "noXHR"));
}
}
我改變了它,但現在我不能提交表單既不 – Simeon 2013-02-28 22:18:32
@Simeon:對不起,我不明白你的意思。請更具體一些。 – Jon 2013-02-28 22:20:59
當我點擊提交按鈕沒有任何反應 – Simeon 2013-02-28 22:22:46