Symfony和阿贾克斯 [英] Symfony and Ajax
问题描述
这是我第一次与阿贾克斯在Symfony的情况下涉足,我可能是一个由code必须如何组织有点困惑。
This is my first time dabbling with Ajax in a Symfony context, and I am possibly a little confused by how the code must be organized.
比方说,我的Ajax调用是这样的:
Let's say my ajax call looks like this:
$.getJSON('module/getMeSomeJSONThroughAjax', function(data) {console.log(data); } )
和我的模块/动作文件夹中,actions.class.php文件里的有一个方法:
and in my module/actions folder, the actions.class.php file has a method:
public function getMeSomeJSONThroughAjax()
{
// do something
return $jsonEncodedString;
}
请记住,我是不是创造上述方法
,所以我没有在执行 $ P $模板PFIX大多数其他行动将。只需怎么我不认为创建一个模板是矫枉过正的一个AJAX HTTP请求只读取JSON数据的情况。
Keep in mind, that I am not creating a template for the above method
, hence I don't have the execute prefix as most other actions would. Simply coz I don't think creating a template is a case of overkill for fetching only JSON data for an AJAX HTTP request.
然而,当发生在我的浏览器触发Ajax调用的事件,我的控制台记录以下内容:
However, upon the event taking place in my browser that triggers the Ajax call, my console logs the following:
> Failed to load resource: the server responded with a status of 404
> (Not Found)
> http://localhost:8080/customerview_dev.php/flashcard/getMeSomeJSONThroughAjax
我察觉这是一个路由的问题。一个可能的办法简单地摆脱这个问题将是创造上述模板,重命名行动 executeGetMeSomeJsonThroughAjax()
。但是就像我提到的,恕我直言,这是矫枉过正,并必须有一个更美观和适当的方式能完成这一操作的。
I am perceiving this to be a routing issue. A possible way to simply get rid of the problem would be to create a template for the above and rename the Action to executeGetMeSomeJsonThroughAjax()
. But like I mentioned, this IMHO is overkill and there has to be a more aesthetically and proper way of getting this done.
你怎么Symfonians做使Ajax调用?
What do you Symfonians do for making Ajax calls ?
推荐答案
您仍然需要执行preFIX,即使你没有一个模板。 您可以设置模板为无,如果你想要的,或者:
You still need the execute prefix, even if you don't have a template. You can set the templates to None if you want, or:
return $this->renderText($jsonEncodeString);
应该工作了。
should work too.
这篇关于Symfony和阿贾克斯的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!