我正在使用Angular和FOSRestBundle
@Injectable({
providedIn: 'root'
})
export class DataService {
constructor(private url: string, private http: HttpClient) { }
create(resource) {
console.log("url: " + this.url);
console.log(resource);
return this.http.post(this.url + "?access_token=YjJkNWU0ZDE5NjgxNGM5MmYzYTYxOGY0YzNmZjgxNDAzY2QzNTA5MmI2NjEyMWMzMDhlM2NlM2JmYzk4ZGQ5Mg", JSON.stringify(resource));
}
}
console.log(资源)
对象{标题:“ gfdsfg”,子弹:“ fdsd”,故事情节:“ fgdsgfd”,摘要:“ gfsfd”,视频源:“ 107”,年份:“ 2011”,长度:“ 44”,状态:“未决 “海报:” 数据:图像/ PNG; BASE64,iVBORw0KGgoAAAANSUhEUgAAAu8AAAL1CAIAAAD4rJ37AAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAAEnQAABJ0Ad5mH3gAAIolSURBVHhe7d17eFZVgud7 / po6z / xxzj / lVPfU9JmeS3fP9G2mj2dmyrYt23lO29Ndly6riXSMICpe8NJahkkMqUjkkooQaMIlJUYilyhGDJEASqQEKyA0yMWgYoHxhhWjZaIlKapeReWs2957rX3L + 4Y3CTt8P4 + PT959WWvttW + / d + / 9bib9H398xVf ++ C + / 8kfiv8vlf // 5L26ZveAsAABARkwKRZk / + V9TBj / + pRkJAABw3puko8z / dXnZVTfdcesddz799NNmDAAAQBaINHP5b31 / 5h0 / KP / BD34w86YZ3S / sNWMAAACyYNJX / ueNt8goc9e137vyX / 7 + N7jNBAAAsmXSJTfcI8ycetW // M9 / 8S9 + / 3 + YwQAAABkx6ep / vOcHP7hz8l / 9xVf + 06WkGQAAkDmTpv7gB3ffffNf / tGlX / lPf06aAQAAmTPp2rvvvuuumy77T5d + 5Q9IMwAAIHtkmvnHf5zxzT / 886 / 8wSWkGQAAkDmT / v72u + 6887ar / idpBgAAZNKk / z7tzjvuuOPGf / j2v / z9b5BmAABA5kz6yjenXX / bHTNn3jL5b // yX5JmAABA1kz6yn + 69Gvfuv6GW2befPPNN143zQwGAADICJFm5BMz / + clk7879fobbrrZDAYAAMgIk2b + xe9 / 41 / 83n / nuRkAAJA5k77yB + bXTKQZAACQRaQZAACQbSr NyNtM / + NF / EfSDAAAyB6RZvRDMyLN / DfSDAAAyBzSDAAAyDY / zfx30gwAAMgi0gwAAMi2oqaZzbMuHs7k5T1m4ni6iFmb9aee5ZND8wy9vLG6 / KH0MmwDbbdfIku85Pa2ATNoRML1uu0cE7m3dy6 / pW6b + RTXOU ... “wideImage:” 数据:图像/ PNG; BASE64,iVBORw0KGgoAAAANSUhEUgAABgEAAAM / CAIAAAB3WuXdAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAAEnQAABJ0Ad5mH3gAAP + lSURBVHhe7N0FfFNXwwbwWN0o0OLu7lLcHQbDxjYYG2wDxhjMv9nL3B0YMhjuPty9eHEo3gIFCqVGLY18T3IudyFNQlra0obn / + 7他/ Xcc8893ptEqdVqFURERERERERElN + 4ublJSwrFmTNn4uPjpRWzgICA6tWrSysKhUr6fyIiIiIiIiIiyrcSExOlpQestnAOiIiIiIiIiIgo3zMYDNLSA1ZbOAdEREREREREROT6OAdEREREREREROT6OAdEREREREREROT6OAdEREREREREROT6OAdEREREREREROT6OAdEREREREREROT6OAdEREREREREROT6OAdEREREREREROT6OAdEREREREREROT6OAdEREREREREROT68vocUFhY2JAhQzZu3CitExERERERERFR5mXnHFBcXNy4ceO + / fbb1NRUadNj8 / DwkP8lIiIiIiIiIsrImJSQ9uMw / f410jrZ4tQc0MGDB2 / fvi2t5K7q1avPnj27TZs20joRERERERER5QG6FX + mjmtj + R + 2SPvyL6NRO + vz9BmfKHRa01rkOe13L6W + 3Tb1 / c66VZMU6aaN + jOh2u + GGG9eMZ + Qnzg1B5SWljZ9 + vQTJ05I60RERERERET01FNVqOPx8TzJZB2KT 1rt8eYf + lN7dVvn4Rh1tabKouV0a6eKeaJ8JBOfBVtjJq1k3r179yZNmvTiiy8OGDBgzJgx + / + fvNxgMYpdOp9u4cePrr7 + OXbJx48bFxcUdO3YMy vWrcNhqamp33777ZQpU86dO / fBBx9g + 9ChQ5ctW4bTRThERERERERElHvc3JWFS / ...”}
您可以看到Angular正在发送正确的数据。
我的控制器操作:
/**
* @FOSRest\Post("/documentary", name="create_documentary", options={ "method_prefix" = false })
*
* @param Request $request
* @return JsonResponse
*/
public function createDocumentaryAction(Request $request)
{
$documentary = new Documentary();
$headers = [
'Content-Type' => 'application/json',
'Access-Control-Allow-Origin' => '*'
];
$form = $this->createForm(AdminDocumentaryForm::class, $documentary);
$data = json_decode($request->getContent(), true);
$form->submit($data);
//return new JsonResponse($data, 200, $headers);
//return new JsonResponse($form->getData(), 200, $headers);
if ($form->isValid()) {
return new JsonResponse($form->getData(), 200, $headers);
} else {
$errors = (string) $form->getErrors(true, false);
return new JsonResponse($errors, 400, $headers);
}
return new JsonResponse($data, 200, $headers);
返回
对象{标题:“ gfdsfg”,子弹:“ fdsd”,故事情节:“ fgdsgfd”,摘要:“ gfsfd”,视频源:“ 107”,年份:“ 2011”,长度:“ 44”,状态:“未决 “海报:” 数据:图像/ PNG; BASE64,iVBORw0KGgoAAAANSUhEUgAAAu8AAAL1CAIAAAD4rJ37AAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAAEnQAABJ0Ad5mH3gAAIolSURBVHhe7d17eFZVgud7 / po6z / xxzj / lVPfU9JmeS3fP9G2mj2dmyrYt23lO29Ndly6riXSMICpe8NJahkkMqUjkkooQaMIlJUYilyhGDJEASqQEKyA0yMWgYoHxhhWjZaIlKapeReWs2957rX3L + 4Y3CTt8P4 + PT959WWvttW + / d + / 9bib9H398xVf ++ C + / 8kfiv8vlf // 5L26ZveAsAABARkwKRZk / + V9TBj / + pRkJAABw3puko8z / dXnZVTfdcesddz799NNmDAAAQBaINHP5b31 / 5h0 / KP / BD34w86YZ3S / sNWMAAACyYNJX / ueNt8goc9e137vyX / 7 + N7jNBAAAsmXSJTfcI8ycetW // M9 / 8S9 + / 3 + YwQAAABkx6ep / vOcHP7hz8l / 9xVf + 06WkGQAAkDmTpv7gB3ffffNf / tGlX / lPf06aAQAAmTPp2rvvvuuumy77T5d + 5Q9IMwAAIHtkmvnHf5zxzT / 886 / 8wSWkGQAAkDmT / v72u + 6887ar / idpBgAAZNKk / z7tzjvuuOPGf / j2v / z9b5BmAABA5kz6yjenXX / bHTNn3jL5b // yX5JmAABA1kz6yn + 69Gvfuv6GW2befPPNN143zQwGAADICJFm5BMz / + clk7879fobbrrZDAYAAMgIk2b + xe9 / 41 / 83n / nuRkAAJA5k77yB + bXTKQZAACQRaQZAACQbSr NyNtM / + NF / EfSDAAAyB6RZvRDMyLN / DfSDAAAyBzSDAAAyDY / zfx30gwAAMgi0gwAAMi2oqaZzbMuHs7k5T1m4ni6iFmb9aee5ZND8wy9vLG6 / KH0MmwDbbdfIku85Pa2ATNoRML1uu0cE7m3dy6 / pW6b + RTXOU ... “wideImage:” 数据:图像/ PNG; BASE64,iVBORw0KGgoAAAANSUhEUgAABgEAAAM / CAIAAAB3WuXdAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAAEnQAABJ0Ad5mH3gAAP + lSURBVHhe7N0FfFNXwwbwWN0o0OLu7lLcHQbDxjYYG2wDxhjMv9nL3B0YMhjuPty9eHEo3gIFCqVGLY18T3IudyFNQlra0obn / + 7他/ Xcc8893ptEqdVqFURERERERERElN + 4ublJSwrFmTNn4uPjpRWzgICA6tWrSysKhUr6fyIiIiIiIiIiyrcSExOlpQestnAOiIiIiIiIiIgo3zMYDNLSA1ZbOAdEREREREREROT6OAdEREREREREROT6OAdEREREREREROT6OAdEREREREREROT6OAdEREREREREROT6OAdEREREREREROT6OAdEREREREREROT6OAdEREREREREROT6OAdEREREREREROT6OAdEREREREREROT68vocUFhY2JAhQzZu3CitExERERERERFR5mXnHFBcXNy4ceO + / fbb1NRUadNj8 / DwkP8lIiIiIiIiIsrImJSQ9uMw / f410jrZ4tQc0MGDB2 / fvi2t5K7q1avPnj27TZs20joRERERERER5QG6FX + mjmtj + R + 2SPvyL6NRO + vz9BmfKHRa01rkOe13L6W + 3Tb1 / c66VZMU6aaN + jOh2u + GGG9eMZ + Qnzg1B5SWljZ9 + vQTJ05I60RERERERET01FNVqOPx8TzJZB2KT 1rt8eYf + lN7dVvn4Rh1tabKouV0a6eKeaJ8JBOfBVtjJq1k3r179yZNmvTiiy8OGDBgzJgx + / + fvNxgMYpdOp9u4cePrr7 + OXbJx48bFxcUdO3YMy vWrcNhqamp33777ZQpU86dO / fBBx9g + 9ChQ5ctW4bTRThERERERERElHvc3JWFS / ...”}
但是$form->getData()
返回此:
对象{}
$form->submit($data);
似乎不起作用。
AdminDocumentaryForm
<?php
namespace App\Form;
use App\Entity\Documentary;
use App\Entity\VideoSource;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class AdminDocumentaryForm extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('title', TextType::class)
->add('slug', TextType::class)
->add('storyline', TextType::class)
->add('summary', TextType::class)
->add('year', IntegerType::class)
->add('length', IntegerType::class)
->add('status', TextType::class)
->add('shortUrl', TextType::class)
->add('videoSource', EntityType::class, [
'class' => VideoSource::class,
'choice_label' => 'id',
])
->add('poster', FileType::class, [
'mapped' => false
])
->add('wideImage', FileType::class, [
'mapped' => false
]);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'csrf_protection' => false,
'data_class' => Documentary::class,
]);
}
public function getName()
{
return "admin_documentary";
}
}
我不知道我在想什么,你能帮忙吗?