我正在向JSON數據發送帶有JSON數據的AJAX POST到Symfony2控制器。如果沒有指定任何數據,控制器會創建一個空記錄並返回成功(無錯誤)。但是,當我在POST中傳遞數據時,它會給我標題中顯示的錯誤。這裏是我的AJAX代碼:無法解碼JSON,語法錯誤 - 格式錯誤JSON
$.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;
您未發送JSON。您使用鍵值對發送正常的HTTP請求。 – lonesomeday
你可以添加你參考的地址實體嗎?我認爲你的JMS配置可能有問題。 (你必須告訴JMS串行器如何反序列化你的JSON) – Stivni
我已經添加了我引用的地址實體的代碼。我已經跳過了setter和getters,以減少粘貼代碼的長度。使用setter,我可以將數據保存到表中,但是我更喜歡序列化它,而無需每次需要保留字段時都調用setter。寂寞的日子,我不明白你的意思,它不是JSON,因爲JSON基本上是密鑰對,我正在開發它。你能指出JSON有什麼問題嗎? –