AJAX请求从Cordova到Symfony收到400错误

时间:2019-06-11 08:32:50

标签: jquery ajax symfony

我必须从Cordova向Symfony提出AJAX请求,但出现“错误请求”错误。

var arr = {
  username: "darkcast",
  password: "test"
};

$.ajax({
  type:'POST',
  url: 'https://******/login2',    
  data: JSON.stringify(arr),
  accepts: "application/json; charset=utf-8",
  contentType: 'application/json,charset=utf-8',
  headers: {
    'Access-Control-Allow-Origin': '*',
  },
  crossDomain: true,    
  dataType: 'json',
  success: function (jqXHR, textStatus, errorThrown) {
    console.log("**************************************");
    console.log("textStatus :" + textStatus);
    console.log("jqXHR :" + jqXHR.statusText);
    console.log("status:" + jqXHR.status);
  }, 
  error: function(jqXHR, textStatus, errorThrown) {
    console.log("textStatus :" + textStatus);
    console.log(JSON.stringify(arr));
    console.log("jqXHR :" + jqXHR.statusText);
    console.log("status:" + jqXHR.status);
  }
});
public function login2(Request $request)
{   
  if ($request->isXMLHttpRequest()) 
  {         
    return new JsonResponse(array('data' => 'this is a json response'));
  }    
  return new Response('This is not ajax!', 400);
}
}

1 个答案:

答案 0 :(得分:0)

我尝试过:

public function login2(Request $request)
{
    $isAjax=$request->isXmlHttpRequest();
    if ($isAjax) {         
        return new JsonResponse(array('data' => 'this is a json response'));
    }

    return new Response('This is not ajax!', 400);
}
}

仍然不起作用