我将带有JSON数据的AJAX POST发送到Symfony2控制器。如果未指定任何数据,控制器会创建一个空记录并返回成功(无错误)。但是,当我在POST中传递数据时,它会给出标题中显示的错误。这是我的AJAX代码:
$.ajax({
dataType: "json",
type: "POST",
data: {
"useruid": "Nothing",
"type": "Office in Space",
"latitude": "12",
"longitude": "100",
"displayed": "true",
"public": "true",
"verified": "true",
"street": "Something",
"city": "Something",
"country": "Space",
"region": "North Space",
"created": "2009-03-07T00:00:00-0500",
"delete_status": "active"
},
url: "/web/app_dev.php/address/",
success: function (responseText)
{
console.log("Request was successful, data received: " + JSON.stringify(responseText));
},
error: function (error) {
alert(JSON.stringify(error));
}
});
要反序列化JSON,这是我的控制器代码:
public function createAction(Request $request)
{
$content = $this->get('request')->getContent();
var_dump($content);
$serializer = $this->get('jms_serializer');
$entity = $serializer->deserialize($content, 'Name\NameBundle\Entity\Address', 'json');
$em = $this->getDoctrine()->getManager();
$em->persist($entity);
$em->flush();
return array(
'entity' => $entity,
'form' => $form->createView(),
);
}
我正在使用JMSSerializer包。它无法解码JSON的原因是什么?我用JSONlint验证了我的JSON,没关系。有什么想法吗?
**编辑代码:添加了实体类**
namespace Factory\WebServicesBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation\Type;
/**
* Address
* @ORM\Table(name="address")
* @ORM\Entity
*/
class Address
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
* @JMS\Serializer\Annotation\Type("integer")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="useruid", type="string", length=200, nullable=true)
* @JMS\Serializer\Annotation\Type("string")
*/
private $useruid;
/**
* @var string
*
* @ORM\Column(name="type", type="string", nullable=true)
* @JMS\Serializer\Annotation\Type("string")
*/
private $type;
/**
* @var string
*
* @ORM\Column(name="latitude", type="string", length=120, nullable=true)
* @JMS\Serializer\Annotation\Type("string")
*/
private $latitude;
/**
* @var string
*
* @ORM\Column(name="longitude", type="string", length=120, nullable=true)
* @JMS\Serializer\Annotation\Type("string")
*/
private $longitude;
/**
* @var boolean
*
* @ORM\Column(name="displayed", type="boolean", nullable=true)
* @JMS\Serializer\Annotation\Type("boolean")
*/
private $displayed;
/**
* @var boolean
*
* @ORM\Column(name="public", type="boolean", nullable=true)
* @JMS\Serializer\Annotation\Type("boolean")
*/
private $public;
/**
* @var boolean
*
* @ORM\Column(name="verified", type="boolean", nullable=true)
* @JMS\Serializer\Annotation\Type("boolean")
*/
private $verified;
/**
* @var string
*
* @ORM\Column(name="street", type="string", length=200, nullable=true)
* @JMS\Serializer\Annotation\Type("string")
*/
private $street;
/**
* @var string
*
* @ORM\Column(name="city", type="string", length=200, nullable=true)
* @JMS\Serializer\Annotation\Type("string")
*/
private $city;
/**
* @var string
*
* @ORM\Column(name="country", type="string", length=200, nullable=true)
* @JMS\Serializer\Annotation\Type("string")
*/
private $country;
/**
* @var string
*
* @ORM\Column(name="locality", type="string", length=200, nullable=true)
* @JMS\Serializer\Annotation\Type("string")
*/
private $locality;
/**
* @var string
*
*
* @ORM\Column(name="region", type="string", length=200, nullable=true)
* @JMS\Serializer\Annotation\Type("string")
*/
private $region;
/**
* @var string
*
*
* @ORM\Column(name="code", type="string", length=100, nullable=true)
* @JMS\Serializer\Annotation\Type("string")
*
*/
private $code;
/**
*
* @var string
*
*
* @ORM\Column(name="pobox", type="string", length=100, nullable=true)
* @JMS\Serializer\Annotation\Type("string")
*
*/
private $pobox;
/**
* @var string
*
*
* @ORM\Column(name="telephone", type="string", length=60, nullable=true)
* @JMS\Serializer\Annotation\Type("string")
*/
private $telephone;
/**
* @var string
*
*
* @ORM\Column(name="cellphone", type="string", length=200, nullable=true)
* @JMS\Serializer\Annotation\Type("string")
*/
private $cellphone;
/**
* @var string
*
*
* @ORM\Column(name="email", type="string", length=200, nullable=true)
* @JMS\Serializer\Annotation\Type("string")
*/
private $email;
/**
* @var string
*
*
* @ORM\Column(name="facsmile", type="string", length=60, nullable=true)
* @JMS\Serializer\Annotation\Type("string")
*/
private $facsmile;
/**
* @var \DateTime
*
*
* @ORM\Column(name="created", type="datetime", nullable=true)
* @JMS\Serializer\Annotation\Type("DateTime")
*/
private $created;
/**
* @var \DateTime
*
*
* @ORM\Column(name="modified", type="datetime", nullable=true)
* @JMS\Serializer\Annotation\Type("DateTime")
*/
private $modified;
/**
* @var string
*
*
* @ORM\Column(name="delete_status", type="string", nullable=true)
* @JMS\Serializer\Annotation\Type("string")
*/
private $deleteStatus;
答案 0 :(得分:2)
您必须指示JMS Serializer如何反序列化您的字段
您可以使用注释来完成此操作,例如@JMS\Serializer\Annotation\Type("string")
JMS Serializer documentation列出了所有可能的类型。
为了清楚起见,您将注释放在实体的每个要反序列化的字段上,例如。
/**
* @var string
*
* @JMS\Serializer\Annotation\Type("string")
* @ORM\Column(name="street", type="string", length=200, nullable=true)
*/
private $street;
希望这会有所帮助......
答案 1 :(得分:2)
问题(至少是当前问题)是JSON 不被发布。因此,请求正文确实不 JSON:userId=..&type=..
等。
这是因为 $.ajax
的data
未序列化为JSON 。而是将对象映射到查询键/值对:
默认情况下,作为对象传入
data
选项的数据(技术上,不是字符串)将被处理并转换为查询字符串,适合默认内容类型“application / x -www-form-urlencoded“..
最简单的解决方法是实际发送JSON:
$.ajax({
dataType: "json", // this is only for the response!
type: "POST",
data: JSON.stringify(theData), // since (JSON) string, won't be processed
// ..
})
请记住,JSON 是文本,JavaScript对象不是JSON!
使用json3.js
或等效的垫片来支持较旧/古老的浏览器。