简体   繁体   English

CakePHP2.0在CakePHP控制器中访问发布变量的正确方法是什么?

[英]CakePHP2.0 What is the correct way to access post variables in a CakePHP controller?

I am unsure as to how I access variables that I have posted using ajax in my controller. 我不确定如何访问在控制器中使用ajax发布的变量。 I though it might be something along the lines of: 我虽然可能是这样的:

$this->request->data['post']['varName'];

I don't think that is the correct way to access the variables I have posted as it doesn't seem to work, so my question is: "What is the correct way to access post variables in a CakePHP controller". 我认为这不是访问我发布的变量的正确方法,因为它似乎不起作用,所以我的问题是:“在CakePHP控制器中访问发布变量的正确方法是什么?” For completeness I will include an example jQuery ajax call. 为了完整起见,我将包括一个示例jQuery ajax调用。 If you could refer how to access the data with the example below that would be great 如果您可以通过下面的示例引用如何访问数据,那将是很好的

$.ajax({ type: "POST",
         url: "someURL", // Not an actual URL just placeholder for example
         data: {'foo': 5, 'bar': 12},
         success: function()
                  {
                       alert('Post was successful');
                  }
          });

So how would I access foo and bar in a cakePHP controller? 那么,如何在cakePHP控制器中访问foo和bar?

Also if you know where to find this information in the documentation could you please link me to it as I had a hard time finding the information. 另外,如果您知道在文档中的哪里可以找到此信息,请联系我,因为我很难找到该信息。

Update! 更新!

Found the link to the documentation here. 此处找到文档的链接

Is $this->request-data['post']['varName']; $this->request-data['post']['varName']; a typo? 错字了吗? If not, then you have a syntax error after the request property where you need a -> . 如果不是,则在request属性后需要->地方会出现语法错误。

I think your problem could be solved by using this though: 我认为您的问题可以通过使用以下方法解决:

echo $this->request->data['foo']; // Should print 5
echo $this->request->data['bar']; // Should print 12

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM