For me - this comes down to necessities.  Is it a necessity to send that
data as a JSON object?  If the answer is no(which I think it is), you can
remove the contentType from your original ajax request, and it would fire
off correctly and be received by web.py just fine.

var my_data = { id: "12345", tags: "tag1,tag2,tag3" }
$.ajax({
  type : "POST",
  url : "/my_url/",
  data : my_data,
});

I dropped dataType because I didn't see a callback function (complete,
success, failure, etc.) handling the server response.  If you are handling
the response, and expecting application/json back from the server - than
you should leave that in so jQuery will parse the server response properly.

I spot tested and something like this works

jQuery.ajax({
 type:"POST",
 url:'/someurl',
 data:{id:'12345',tags:'tag1,tag2,tag3'},
 success:function(data){
   console.log(data);
   }
 });

recv'd by:
class SomeUrlClass: def POST(self): buffer = 'Unable to process'  data =
web.input()
 id = data.get('id','No Id')
 tags = data.get('tags','').split(',')
 return "id: %s, tags: %s" % (id, ','.join(tags))


On Mon, Jan 14, 2013 at 11:07 AM, Shannon Cruey <
[email protected]> wrote:

> I believe you're hitting the issue I meant to express with that
> not-very-helpful comment.  You said you wanna write some json, but you're
> still defining my_data as a javascript object.  Look at line 38 of
> ajaxdropdowns.js for a better documented example.
>
> You've defined my_data as a *javascript object*, but the jQuery ajax
> function likely is talking about a *json object)*  (a STRING representation
> of your js object).
>
> I usually use the browsers built-in json to seralize the javascript object
> into a string, like this:
>
>
> var my_data = { id: "12345", tags: "tag1,tag2,tag3" }
> $.ajax({
>   type : "POST",
>   url : "/my_url/",
>   data : JSON.stringify(my_data),
>
>   contentType : "application/json; charset=utf-8",
>   dataType : "json",
> });
>
> Now, if you truly want to construct my_data as json, it has to be a
> string, so this should work too.  (Notice the quotes on my_data :-)
>
>
> var my_data = '{ id: "12345", tags: "tag1,tag2,tag3" }'
> $.ajax({
>   type : "POST",
>   url : "/my_url/",
>   data : my_data,
>   contentType : "application/json; charset=utf-8",
>   dataType : "json",
> });
>
> Yes, this very thing bit me several times until I got my head around it.
> Good luck :-)
>
>
> On Fri, Jan 11, 2013 at 3:19 PM, * <[email protected]> wrote:
>
>> On Fri, Jan 4, 2013 at 3:11 AM, Zagfai Kwong <[email protected]> wrote:
>> > No matter GET or POST, you could use web.input()
>> >
>> > Brendan Sleight於 2012年12月26日星期三UTC+8下午7時43分41秒寫道:
>> >>
>> >>
>> >>
>> >> On Friday, 14 December 2012 16:14:06 UTC, NSC wrote:
>> >>>
>> >>> Hi everyone...
>> >>>
>> >>> Check it out at:
>> https://github.com/shannoncruey/webpy-jquery-sampleapp
>> >>>
>> >>> Hope it helps someone!
>> >>
>> >>
>> >> Very helpful to me. As a beginner in wep.py (<4 days) it also shows
>> some
>> >> good practice with include js and css files.
>> >>
>> >> IMHO - Good addition to the cookbook examples.
>> >>
>> >> Thanks,
>> >> Brendan
>> >
>> > --
>> > You received this message because you are subscribed to the Google
>> Groups
>> > "web.py" group.
>> > To view this discussion on the web visit
>> > https://groups.google.com/d/msg/webpy/-/Q5sZlYyaJDUJ.
>> >
>> > To post to this group, send email to [email protected].
>> > To unsubscribe from this group, send email to
>> > [email protected].
>> > For more options, visit this group at
>> > http://groups.google.com/group/webpy?hl=en.
>>
>> Thanks, this sample app is helping me a lot!!! :-)
>>
>> Re: "jquery ajax args must be a JSON string" [0]
>>
>> As far as I can tell, the data setting of jQuery's .ajax doesn't need
>> to necessarily be a string. For instance the docs [1] say: "Object
>> must be Key/Value pairs."
>>
>> For what I'm doing, it makes more sense to write some json and send it
>> like this...
>>
>> var my_data = { id: "12345", tags: "tag1,tag2,tag3" }
>> $.ajax({
>>   type : "POST",
>>   url : "/my_url/",
>>   data : my_data,
>>   contentType : "application/json; charset=utf-8",
>>   dataType : "json",
>> });
>>
>> However, this is not working...
>> Is there something that needs to be changed in the getAjaxArg function
>> [2] for this to work???
>>
>> Hope this makes some sense :-)
>>
>> [0]
>>
>> https://github.com/shannoncruey/webpy-jquery-sampleapp/blob/master/static/script/ajaxdropdowns.js#L14
>> [1]
>> http://api.jquery.com/jQuery.ajax/
>> [2]
>>
>> https://github.com/shannoncruey/webpy-jquery-sampleapp/blob/master/main.py#L77
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "web.py" group.
>> To post to this group, send email to [email protected].
>> To unsubscribe from this group, send email to
>> [email protected].
>> For more options, visit this group at
>> http://groups.google.com/group/webpy?hl=en.
>>
>>
>  --
> You received this message because you are subscribed to the Google Groups
> "web.py" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/webpy?hl=en.
>

-- 
You received this message because you are subscribed to the Google Groups 
"web.py" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/webpy?hl=en.

Reply via email to