0

I am trying to use AJAX to call a python file which returns a basic "hello" back to HTML file.

The Problem I'm facing is, the 'error' section of the AJAX gets called even after successful execution,

AJAX request

$.ajax({
    'url': 'http://localhost/keywordSearch.py',
    'success': function(response){
        alert(response.responseText);
    },
    'error': function(error){
        $('.table-responsive').html('');
        $().toastmessage('showToast', {
                      text     : 'Unable to get data now. Please try after sometime.',
                      sticky    :  false,
                      stayTime :  2000,
                      position : 'middle-center',
                      type     : 'warning'
        });
        console.log(error.responseText);
    }

});

Python file:sample.py

#!/Python27/python

print "Content-type: text/html;charset:utf-8"
print

print "hello"

The output we get in html console is "hello", indicating that it's coming from 'error', if it came through alert, it would mean 'success'.

I don't understand why 'error' is executed even for a basic program like this, Thanks for the help

SpoonMeiser
  • 19,918
  • 8
  • 50
  • 68
  • open the `debugger of explorer`, click `network` to see the `Response Headers` of `http://localhost/keywordSearch.py` , `status 2xx/3xx ?` – Cheney Apr 26 '17 at 11:15
  • status 200 OK @Cheney – Vishwanth Iron Heart Apr 26 '17 at 11:20
  • have you get this in your console, `XMLHttpRequest cannot load http://localhost:8000/web/show. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://127.0.0.1:8000' is therefore not allowed access.` or in the other word, your html's url is not like `http://localhost/xxx`, if it is, see [Access-Control-Allow-Origin](http://stackoverflow.com/questions/20035101/no-access-control-allow-origin-header-is-present-on-the-requested-resource) ; or you can use a local url with static html page to see which part in error. – Cheney Apr 26 '17 at 13:46

0 Answers0