1

I have written a code in bottle in python which gets data from mongodb and when the user request the url http://localhost:8080/index/test from bottle it will return the json result from mongoDB. it works fine when I point my browser to that url, I can see all the result on the browser.

However when I try to send a request from jQuery ajax I always get error, and the request never succeeds.

Has anyone ever done anything similar who can share their approach with me?

MY general question is, what the best way to get data from MongoDB from client side, when using bottle as the server. I have seen some example in Node.js but I want to use python as the server.

I have used this code.

                   $.ajax({
                    type: "POST",
                    url: "http://localhost:8080/hello/test",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",                        
                    success: function (response) {

                        console.log("success");
                    },
                    error: function (response){

                        console.log("failed");
                    }
                });*/

And I have also tried this :

                $.post( "http://localhost:8080/hello/test", d)
                  .done(function( response ) {
                      console.log("success");
                  });

no luck with any of these. I have also tried GET instead of post, but no luck.

This is kind of what I have in python side :

from bottle import route, run, template

@route('/hello/<name>')
def index(name):
    return {'status':'online', 'something':'blah blah'}

run(host='localhost', port=8080) 

Many thanks in advance.

Zardaloop
  • 1,594
  • 5
  • 22
  • 43
  • Could you paste the jquery AJAX code which you are using? – Lalit Agarwal Apr 17 '14 at 09:15
  • many thanks @LalitAgarwal for your comment. I have updated my post with the ajax codes that I have used. – Zardaloop Apr 17 '14 at 09:50
  • Is the Python definitely returning JSON? What is the response you see when it errors. – sWW Apr 17 '14 at 10:20
  • Check gulty's answer, I believe it is a CORS issue. If your app is not running on 8080, you won't be able to access to 8080 port unless you allow cors. Check your browser's complain (if it s chrome ctr shift j will launch the dev tools), I believe you ll see something like: `No 'Access-Control-Allow-Origin' header is present on the requested resource.` – anvarik Apr 17 '14 at 11:02
  • @sWW yes it is returning the JSON, when I go to browser and type it the URL it will show me a page full of JSON objects. – Zardaloop Apr 17 '14 at 13:12
  • If you put a breakpoint on `console.log("failed");` what is in `response`? – sWW Apr 17 '14 at 13:13
  • @anvarik my app is running on port 8080 its a simply setup, I have added the code to my question above. Many thanks for your help. – Zardaloop Apr 17 '14 at 13:16
  • @sWW it only says status="error" pretty much it, nothing else, that's why I can't even figure out why it is not working. :( – Zardaloop Apr 17 '14 at 13:17
  • I've not used Bottle myself but looking at `http://bottlepy.org/docs/dev/index.html` it seems to suggest that you should use `http://localhost:8080/hello/test` however you said it works when you navigate directly to `http://localhost:8080/index/test` so I'm stumped. – sWW Apr 17 '14 at 13:25
  • @sWW yest that's exactly the case, that's why I am not getting it!!! it has something to do with the jQuery ajax I guess. since there is no other port etc involved. – Zardaloop Apr 17 '14 at 13:28
  • Out of curiosity what happens if you do use `http://localhost:8080/hello/test`? – sWW Apr 17 '14 at 13:29
  • @sWW obviously you will get ERORR 404 in bottle @route('/hello/') mean if the url is pointing at URL/hello and it is passing a vallue name :) – Zardaloop Apr 17 '14 at 13:35
  • Well you have `@route('/hello/')` in your code and you aren't using `URL/hello` you are using `URL/index` so I'm a bit confused as to how it should work. – sWW Apr 17 '14 at 13:44
  • @sWW sorry mate I made a typo in my question I have fixed that. Sorry for confusing you. – Zardaloop Apr 17 '14 at 14:23

1 Answers1

2

First of all 'GET' is the better alternative since you are not passing any params to your DB.

Secondly on which port is your application running? You are adding 8080 to your request which lets me assume your app is running under a different port. JS is based on the Same Origin Policy that means if you want to access data from a different URL (different port = different url) it won't give you any repsponse data.

To make this work either make sure the python script is passing the information to your application directly or you have to implement Cross Origin Ressource Sharing. To do this you have to add a header to the response of your python (port 8080) script with the following content

Access-Control-Allow-Origin: localhost:XXXX //replace XXXX with your application port

EDIT: If you need to know how to activate COR check out this thread on stackoverflow: PY Bottle enable COR

Community
  • 1
  • 1
gulty
  • 1,068
  • 1
  • 8
  • 14
  • Many thanks @gulty, bottle is running on port 8080 and I simply instead of returning the template I retun a json array. I have edited my question. I am not really trying to access any other port. – Zardaloop Apr 17 '14 at 13:07
  • So if you are not trying to use a different port, then you main application (not bottle) is running under localhost:8080/hello? how about using a non static url then? like /test if you are on hello already? same error? – gulty Apr 17 '14 at 15:14
  • emmm I don't know I need to go home and try these, I will let you know in few hours :) let me get this right, should I use something like this: $.ajax({ url:'http://localhost:8080/hello/test', dataType:'jsonp', crossDomain: 'true', success:function (data) {alert("success");} }); – Zardaloop Apr 17 '14 at 15:32
  • You need to figure out what's your applications home directory. where is the file accessing your javascript located? if it is localhost:8080/hello (for example) you don't need to create a jsonp request. You dont even need the ajax url with localhost - you can just use url:'/test'. If your application isn't using the same domain/port as your bottle db application you have to activate COR. I just found a link for you (see edit) – gulty Apr 17 '14 at 16:11
  • That was it, it finally worked. Many thanks for your help. You are a life saver :) – Zardaloop Apr 17 '14 at 20:01