我正在做一个使用Symfony 2的项目,我已经构建了一个包来处理我所有的数据库服务,这些服务来回传递JSON数据。
我的问题/问题:
json={"key":"value"}
来欺骗ajax调用的普通表单post。如果我不给它命名,我似乎不能从Symfony请求对象$JSON = $request->request->get('json');
总结一下:
如果我做错了,请随时告诉我!
发布于 2012-03-05 19:21:50
如果您想在控制器中检索已在请求体中作为标准JSON发送的数据,您可以执行类似以下操作:
public function yourAction()
{
$params = array();
$content = $this->get("request")->getContent();
if (!empty($content))
{
$params = json_decode($content, true); // 2nd param to get as array
}
}
现在,$params
将是一个充满您的JSON数据的数组。删除json_decode()
调用中的true
参数值以获取stdClass
对象。
发布于 2015-04-15 21:16:32
我写了一个方法来获取数组形式的内容
protected function getContentAsArray(Request $request){
$content = $request->getContent();
if(empty($content)){
throw new BadRequestHttpException("Content is empty");
}
if(!Validator::isValidJsonString($content)){
throw new BadRequestHttpException("Content is not a valid json");
}
return new ArrayCollection(json_decode($content, true));
}
我使用这个方法,如下所示
$content = $this->getContentAsArray($request);
$category = new Category();
$category->setTitle($content->get('title'));
$category->setMetaTitle($content->get('meta_title'));
发布于 2012-03-02 02:53:57
页面上的javascript:
function submitPostForm(url, data) {
var form = document.createElement("form");
form.action = url;
form.method = 'POST';
form.style.display = 'none';
//if (typeof data === 'object') {}
for (var attr in data) {
var param = document.createElement("input");
param.name = attr;
param.value = data[attr];
param.type = 'hidden';
form.appendChild(param);
}
document.body.appendChild(form);
form.submit();
}
在一些事件之后(比如点击"submit"):
// products is now filled with a json array
var products = jQuery('#spreadSheetWidget').spreadsheet('getProducts');
var postData = {
'action': action,
'products': products
}
submitPostForm(jQuery('#submitURLcreateorder').val(), postData);
在控制器中:
/**
* @Route("/varelager/bestilling", name="_varelager_bestilling")
* @Template()
*/
public function bestillingAction(Request $request) {
$products = $request->request->get('products', null); // json-string
$action = $request->request->get('action', null);
return $this->render(
'VarelagerBundle:Varelager:bestilling.html.twig',
array(
'postAction' => $action,
'products' => $products
)
);
}
在模板中(在我的示例中为bestilling.html.twig):
{% block resources %}
{{ parent() }}
<script type="text/javascript">
jQuery(function(){
//jQuery('#placeDateWidget').placedate();
{% autoescape false %}
{% if products %}
jQuery('#spreadSheetWidget').spreadsheet({
enable_listitem_amount: 1,
products: {{products}}
});
jQuery('#spreadSheetWidget').spreadsheet('sumQuantities');
{% endif %}
{% endautoescape %}
});
</script>
{% endblock %}
好了,我想这就是你想要的:)
编辑要在不模拟表单的情况下发送内容,可以使用jQuery.ajax()。下面是一个与上面相同的示例,它不会触发页面刷新。
jQuery.ajax({
url: jQuery('#submitURLsaveorder').val(),
data: postData,
success: function(returnedData, textStatus, jqXHR ){
jQuery('#spreadSheetWidget').spreadsheet('clear');
window.alert("Bestillingen ble lagret");
// consume returnedData here
},
error: jQuery.varelager.ajaxError, // a method
dataType: 'text',
type: 'POST'
});
https://stackoverflow.com/questions/9522029
复制相似问题