63072

Is it possible to post an object from jquery to bottle.py?

here is the jquery

$.ajax({ type: "POST", url: "/posthere", dataType: "json", data: {myDict:{'1':'1', '2':'2'}}, success: function(data){ //do code } });

Here is the python

@route("/posthere", method="POST") def postResource(myDict): #do code return "something"

It looks like the the url formats on support int, float, path, and re... am I missing something?

Answer1:

There are only bytes on the wire. To send an object you need to serialize it using some data format e.g., json:

$.ajax({ type: "POST", url: "/posthere", data: JSON.stringify({myDict: {'1':'1', '2':'2'}}), contentType: "application/json; charset=utf-8", dataType: "json", success: function(data){ alert(data); }, failure: function(err) { alert(err); } });

On the receiving end you need to parse json text into Python object:

from bottle import request, route @route("/posthere", method="POST") def postResource(): #do code myDict = request.json['myDict'] return {"result": "something"}

Returned dictionaries are automatically converted to json.

Answer2:

It might not be that relevant, but the answer in short is No and Yes. If you're using the data attribute of jquery. It will actually transform your object into fields. Something like that:

{ friend: [1,2,3] }

May be sent as such to the server:

friend[0]=1&friend[1]=2&friend[2]=3

That said, HTTP doesn't actually define any "right" way to send data to the server. jQuery does that to make it works as if the client posted a form. So it is serializing data like formdata.

But that's not all! Since you can send data as you like. You can do something different. I'm not sure it is possible to send raw data with jQuery.

You might want to try that:

$.ajax({ url: ..., data: myObject.toJSON(), ... })

Since you're sending a string without any defined fields. You'll have to check on the server for the raw data. Convert the JSON string to a dict and you're good to go.

To make sending json to your server, there is a fabulous thing called, jsonrpc.

http://www.jsonrpc.org/

Unfortunately my knowledge of bottle.py is close to 0 so I can't really help much on how to get data.

<strong>tldr</strong>

Send json to the server and then parse it back to a dict, you could send anything else as long as you know how to parse it back on the other side. (xml, json, messagepack...)

Recommend

  • Wiring multiple independent Spring Applications using Maven
  • How to create an actor in a clustered configuration in F#
  • java rest client: jax-rs & automatic type mapping
  • Which PHP RPC (XML or JSON) library have you successfully used?
  • Spring integration warning Referenced bean 'org.springframework.scheduling.support.PeriodicTrig
  • Does ruby on rails have client side validation like ASP.NET MVC
  • how do i fix broken encoding in my webapp?
  • How to download attachment from gmail in C# using IMAP?
  • Google script gmail addon update TextInput value,on change function
  • toString() for each element of an array in Javascript [duplicate]
  • JavaScript - Can we set javascript objects in cookies?
  • Function insertMany() unordered: proper way to get both the errors and the result?
  • jQuery validate: submitting form only when fields are validated
  • LCD Programming with Arduino
  • java.lang.IllegalStateException: getInputStream() has already been called for this request + Struts2
  • Whats the best way of persisting data to Isolated Storage on Windows Phone 7?
  • PDF using WCF Restful Services
  • openssl handshake failed
  • Can I read an iPhone beacon with Windows.Devices.Bluetooth.Advertisement.BluetoothLEManufacturerData
  • Can't delete or rename original file after resizing
  • chrome.tabs.executeScript only fires when the Developer Console is open
  • copying resource to sdcard gives a damaged file in android
  • Play WS (2.2.1): post/put large request
  • Spark fat jar to run multiple versions on YARN
  • Is there a javascript serializer for JSON.Net?
  • Spring Data JPA custom method causing PropertyReferenceException
  • Different response to non-authenticated users and AJAX calls
  • C# - Serializing and deserializing static member
  • Sending data from AppleScript to FileMaker records
  • Fill an image in a square container while keeping aspect ratio
  • Build own AppleScript numerical error handling
  • Font Awesome Showing Box instead of Icons
  • Properly structure and highlight a GtkPopoverMenu using PyGObject
  • Warning: Can't call setState (or forceUpdate) on an unmounted component
  • How to include full .NET prerequisite for Wix Burn installer
  • Why joiner is not used after Sequence generator or Update statergy
  • Exception on Android 4.0 `android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode)`
  • File not found error Google Drive API
  • Is it possible to post an object from jquery to bottle.py?
  • Python/Django TangoWithDjango Models and Databases