gpt4 book ai didi

ajax - AngularsJS POST JSON 数据到 Symfony2

转载 作者:行者123 更新时间:2023-12-01 08:12:01 25 4
gpt4 key购买 nike

我想知道为什么这不起作用,我有一个 AngularJS 应用程序女巫通过 AJAX 数据发送到 Symfony2 应用程序。可以看到,数据是在我的网络控制台发送的

<?php

namespace Supbox\CloudBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\JsonResponse;
class FolderController extends Controller
{
public function createAction(){
$post = $this->getRequest()->request;
$name = $post->get("name");
$folder = $post->get("folder");
var_dump($post);
die;
}
}

AngularJS 代码

    $http({
method: 'POST',
url: route.folder.create,
data: {
folder: $scope.id,
name: name
}
})

Opera 网络控制台输出

Request URL:http://localhost/supbox/web/box/folder/create
Request Method:POST
Status Code:200 OK
Request Headersview source
Accept-Encoding:gzip,deflate,lzma,sdch
Accept-Language:fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4
Connection:keep-alive
Content-Length:25
Content-Type:application/json;charset=UTF-8
Host:localhost
Origin:http://localhost
Referer:http://localhost/supbox/web/box/
User-Agent:Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.154 Safari/537.36 OPR/20.0.1387.82
Request Payloadview source
{folder:1, name:Ang}
Response Headersview source
Connection:Keep-Alive
Content-Length:431
Content-Type:text/html
Date:Mon, 24 Mar 2014 13:25:53 GMT
Keep-Alive:timeout=5, max=100
Server:Apache/2.4.4 (Win64) OpenSSL/1.0.1d PHP/5.4.12
X-Powered-By:PHP/5.4.12

最佳答案

如果您 (Angular JS) 通过 header 将数据作为 JSON 发布,您需要像这样更改代码:

public function createAction(){
$post = $this->getRequest()->getContent();
$post = json_decode($post);
$name = $post->name;
$folder = $post->folder;
var_dump($post);
var_dump($name); // null
var_dump($folder); // null
die;
}

关于ajax - AngularsJS POST JSON 数据到 Symfony2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22610986/

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