Getting json in bulb from jquery ajax

I need help getting json data in a jar from jquery ajax.

Below is client javascript, which is called when any button is clicked.

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
    function change_status(item, status) {
          var statusObj={"desc":item, "status":status};
          $.ajax({
                    url:"/robot/api/"+item,
                    type: "PUT",
                    contentType:"applicaton/json",
                    dataType:"json",
                    data: JSON.stringify(statusObj)
          });
    }
</script>

server side (flask), I have the code below to catch json data.

@app.route('/robot/api/<item>', methods = ['PUT'])
def update_item(item):

print "content_type: ", request.content_type

print "request.json: ", request.json

if not request.json:
    print "bad json format"
    abort(400)
else
    """
    do something like update data
    """

On a PC, when a button is pressed in a browser that calls the javascript function, the flag server prints the following message:

"content_type:  applicaton/json; charset=UTF-8

request.json:  None

bad json format

192.168.1.241 - - [12/Feb/2014 15:06:16] "PUT /robot/api/3g HTTP/1.1" 400 -"

It shows that request.content_type is json, but when actually printed, it indicates None.

I did a packet capture to see data from a PC with wirehark, and I see below:

From PC to Flask Server:

PUT /robot/api/3g HTTP/1.1

Host: "my flask server ip address"

User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:26.0) Gecko/20100101 Firefox/26.0

Accept: application/json, text/javascript, */*; q=0.01

Accept-Language: en-US,en;q=0.5

Accept-Encoding: gzip, deflate

Content-Type: applicaton/json; charset=UTF-8

X-Requested-With: XMLHttpRequest

Referer: "my flask server ip address"

Content-Length: 28

Connection: keep-alive

{"desc":"3g","status":"off"}

But the flag server returns "HTTP / 1.0 400 BAD REQUEST"

+3
source share
1 answer

, "i" "application/json". jQuery contentType:"application/json" ( contentType:"applicaton/json"), .

JSON application/json - , JSON, request.get_json(force=True)

+6

All Articles