SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Jan 2013
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Send httpresponse to client

    Hi all,
    new here

    i have this python function
    Code Python:
    def gtest(request):
        p = request.POST
        print p
        g = p['information']
        print g
        to_json = {
            "key1": "value1",
            "key2": "value2"
        }
       jsonValidateReturn = simplejson.dumps({"jsonValidateReturn": "ddddd"})
        return HttpResponse(jsonValidateReturn, content_type='application/json' ,mimetype='application/json')

    and on the client side
    Code Javascript:
    $.ajax({
            url: "http://127.0.0.1:8000/api/gtest/",
            type: "POST",
            data: { information : "You have a very nice website, sir."},
            dataType: "json",
            success: function(data) {
            	alert ("post is success");
            },
            error: function(request,error) 
            { 
            alert(request.responseText);
            alert(error);
            }
        });

    no matter what i do when the post is back it goes to error.
    on my server i see this message
    "POST /api/gtest/ HTTP/1.1" 200 31
    so it looks like response should work but no matter what it does not go to the success function..

    plz help :\

    thx
    Guy
    Last edited by Mittineague; Feb 10, 2013 at 15:54. Reason: reformatting bbcode tags

  2. #2
    om nom nom nom Stomme poes's Avatar
    Join Date
    Aug 2007
    Location
    Netherlands
    Posts
    10,233
    Mentioned
    47 Post(s)
    Tagged
    1 Thread(s)
    Could you send a message to a moderator (anyone whose name is green or blue) and ask to have your code edited? Or post again, but wrap your code in code tags ([code] to start, close it with [/code]). Otherwise the forums eat up your indentation which with Python we need. :)

    Also: Python 2x or 3x? Are you using httplib?

    Code:
    return HttpResponse(jsonValidateReturn, content_type='application/json' ,mimetype='application/json')
    What are the required arguments for this (I see a different list on the library pages at docs.python.org for version 2)? I ask because MIMEtype should be represented by Content Type as far as the browser is concerned.

    When you run your Javascript, in your browser debugger, can you see anything else being returned? What errors are being checked in the
    success: function(data) {
    alert ("post is success");
    },
    error: function(request,error)
    {
    alert(request.responseText);
    alert(error);
    }

    section? Those success and errors should refer to the ajax readyStates I think.
    You may not be getting a readyState 4. Here are the states, and you should be able to see which one you get in your browser debugger as you step through your Javascript.

    Quote Originally Posted by ibm article
    0: The request is uninitialized (before you've called open()).
    1: The request is set up, but not sent (before you've called send()).
    2: The request was sent and is in process (you can usually get content headers from the response at this point).
    3: The request is in process; often some partial data is available from the response, but the server isn't finished with its response.
    4: The response is complete; you can get the server's response and use it.
    Also back when I was doing ajax by hand I needed to add in a send() call just to make Opera happy, and I dunno if Opera fixed this or if jQuery (is that what you're using) already fixes this.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •