2

i'm working with python and js on a simple website. i'm trying to call a method from the client side and get result, but no matter what i do success function isnt happening.

this is my JS

 $.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);
        }
    });

this is my server side code

def gtest(request):
    jsonValidateReturn = simplejson.dumps({"jsonValidateReturn": "ddddd"})
    return HttpResponse(jsonValidateReturn, content_type='application/json', mimetype='application/json')   

The server responds to the call - "POST /api/gtest/ HTTP/1.1" 200 31

tried to go over similar questions here but with no success :\ no matter what I do, only the error function is called. the error alert is always empty.. no actual message.

I know this is probably a small change but I can't find it.

guytamir1
  • 73
  • 2
  • 12
  • Where is the `.ajax()` call being triggered from? – Daniel Roseman Jan 29 '13 at 17:03
  • Don't you think it would help if we knew what things are being alerted in the `error` callback? Usually, when you get a `200` status back but it goes to `error`, that means there was a problem with the parsing/returned data. – Ian Jan 29 '13 at 17:04
  • do you know if gtest is being run? – Hoopdady Jan 29 '13 at 17:05
  • the .ajax() is call from a simple "onclick" function.. the error is always empty.. i know message gtest is run becuase i printed to the console and it printed.. – guytamir1 Jan 29 '13 at 17:06
  • Could there be a cross-domain issue? There would be if you're running the JS from `localhost` and sending the request to `127.0.0.1`. What is your response code in `request.status`? If it is `0`, that indicates a cross-domain error; if it is `200` then it's not. – apsillers Jan 29 '13 at 17:09
  • Is there anything in your JavaScript console? – mccannf Jan 29 '13 at 17:09
  • You don't seem to call any specific page ? Could it be the problem ? – poudigne Jan 29 '13 at 17:12
  • apsillers - the status is 0. i changed the url in the .ajax to: "http://localhost:8000/api/gtest/" and status is still 0. in the JavaScript console i get : XMLHttpRequest cannot load http://localhost:8000/api/gtest/. Origin null is not allowed by Access-Control-Allow-Origin. – guytamir1 Jan 29 '13 at 17:15
  • CORS error. Read up [on it here](http://stackoverflow.com/questions/3595515/xmlhttprequest-error-origin-null-is-not-allowed-by-access-control-allow-origin). Either add a proxy to your PHP, set the `Access-Control-Allow-Origin` header on your server to allow cross domain requests, or switch to JSONP.... – mccannf Jan 29 '13 at 17:29
  • You are running the script on a `file:` page, yes? If you had mentioned that error in the first place (or done a [Google search for it](http://www.google.com/search?rlz=1Y3NDUG_enUS503US503&aq=f&client=tablet-android-asus-nexus&sourceid=chrome-mobile&ie=UTF-8&q=Origin+null+is+not+allowed+by+Access-Control-Allow-Origin)) you'd have you solution almost immediately. Duplicate:http://stackoverflow.com/questions/3595515/xmlhttprequest-error-origin-null-is-not-allowed-by-access-control-allow-origin – apsillers Jan 30 '13 at 03:12

2 Answers2

0
 $.ajax({
    url: "http://127.0.0.1:8000/api/gtest/",
    type: "POST",
    data: { 
         'information' : "You have a very nice website, sir.",
         'csrfmiddlewaretoken': '{{csrf_token}}'
    },
    contentType: "application/json;charset=utf-8",
    dataType: "json",
    success: function(data) {
        alert ("post is success");
    },
    error: function(request,error) { 
        alert(request.responseText);
        alert(error);
    }
});
catherine
  • 22,492
  • 12
  • 61
  • 85
0

i cant upvote mccannf's comment.

The problem was solved by the link he posted, i ran the html code from a file on my pc and i needed to load it from the server so link wont start with file:// but with http://

best regards..

Community
  • 1
  • 1
guytamir1
  • 73
  • 2
  • 12