gpt4 book ai didi

javascript - AngularJS 将 Ajax 请求 POST 到 Symfony Controller

转载 作者:行者123 更新时间:2023-12-02 17:46:54 25 4
gpt4 key购买 nike

我正在尝试从我的 angularJs Controller 向我的 Symfony Controller 发出 Ajax 请求。但是,由于未知原因,我无法在 Symfony Controller 中接收数据。我的 Controller 被调用,我可以返回一些信息,我将在 AngularJS 端的成功函数中看到这些信息。但是,我通过 AngularJs 发送的数据无法在 Symfony Controller 上检索。

这是我在 AngularJS 方面所做的事情:

$http.post('{{ path('admin_ima_processmanagement_project_save', {'id':object.id}) }}',{"projectJson":"test"}).
success(function(data, status, headers, config) {
console.log("yeah");
console.log(data);
}).
error(function(data, status, headers, config) {
// called asynchronously if an error occurs
// or server returns response with an error status.
console.log("oh non");
console.log(data);
});

我可以在执行此请求后在控制台中看到“yeah”出现。

在我的 Symfony Controller 中,我有以下内容:

$request = $this->container->get('request');

$projectJson = $request->query->get('projectJson');

$response = array("code" => 100, "success" => true, "projectJson" => $projectJson);

return new Response(json_encode($response));

在控制台上,调用后,我得到 {"code":100,"success":true,"projectJson":{}} ,这意味着 projectJson 不幸为空...

我应该如何检索从客户端发送的数据?&

最佳答案

在类Request属性中query引用GET参数。

在您的情况下,您需要访问 POST 参数,这些参数位于 request 属性中。

所以你的代码应该是这样的:

$projectJson = $request->request->get('projectJson');

有关请求的更多信息,您将找到here .

关于javascript - AngularJS 将 Ajax 请求 POST 到 Symfony Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21666001/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com