The solution was that I needed to get rid of the following in my YUI 3 code:
headers: {
'Content-Type': 'application/json'
}
Woohoo!! I can now send and receive JSON data :-D
On Sunday, December 30, 2012 8:55:34 PM UTC-8, b00m_chef wrote:
>
> This might be a web2py bug...not sure...
>
> I have been trying to get web2py to read a post request sent from
> javascript, but without any success. I hope you guys can help figure out
> what is going on. Note that request.env.content_length is the correct
> length of the data being passed. However, it fails to show up in the
> request.post_vars...what's going on? I should also point out that if I
> change the method to "GET", then I can see the data in the request.vars and
> request.get_vars without any problems. Is there a way to access the header
> date directly (skipping request.vars)?
>
> Also note that jQuery.ajax() with the same data below works fine. Here is
> jQuery version:
> jQuery.ajax(
> {
> type: "POST",
> url: 'data', //python function
> data: {array: 1234},//data sent to server
> dataType: 'json',
> error: function(msg){alert(msg);},
> success: function(data){
> alert(data);
> },
> timeout: 2000
> }
> );
>
>
>
>
> Here is the YUI 3 code:
>
> View:
> <script type="text/javascript">
>
> YUI().use("json-stringify", "io", "dump", "json-parse", function(Y) {
> Y.io('data', {
> method: 'POST',
> data: {array: 1234},
> on: {
> success: function(id, o) {
> var jsonResponse = Y.JSON.parse(o.response)
> Y.log("RAW JSON DATA: " + o);
> Y.log("json data: " + o.responseText);
> Y.log("header:" + jsonResponse.header);
> Y.log("status:" + o.statusText);
> Y.log("keys:" + Object.keys(o));
> Y.log("id:" + id);
> Y.one("#output").set("text", o.response);
> },
> failure: function(x, o) {
> alert("Async call failed!");
> }
> },
> headers: {
> 'Content-Type': 'application/json'
> }
> })
>
> });
> -->
> </script>
>
> <p>Type something and press the button.
> The last 10 entries will appear sorted in a table below.</p>
>
> <INPUT id="qtr" type="text" name = "q" value="web2py"/>
> <button id="readygo">OK</button>
>
> <br/>
> <ul id="out">
> </ul>
>
> Controller:
> @service.json
> def data():
> if request.vars:
> for var in request.vars:
> data[var] = []
> data[var].append(request.vars[var])
> return data
>
>
>
>
--