1

I am a noob to jquery and I am stuck on this ajax request. I have a server on my local network running python service. When I hit the server through the browser with the request

http://192.168.0.109:5000/api/environment?seconds=10

I get the JSON I expect shown in the browser. So I know the server is working ok and responding to request OK

{
"Bedroom Light": {
    "host": "192.168.0.121", 
    "model": "Belkin Plugin Socket 1.0", 
    "name": "Bedroom Light", 
    "serialnumber": "221323K1300027", 
    "state": 0, 
    "type": "LightSwitch"
}

However, when trying to do this through Jquery - I get a jQuery - SyntaxError: Unexpected token : error in the browser console. Here is my Jquery

 $(function () {
  $.ajax({
    url: 'http://192.168.0.109:5000/api/environment?seconds=10',
    type: 'GET',
    crossDomain: true,
    dataType: 'jsonp',
    success: function(data) {
      console.log('sucess',data);
      }
    });
  });

and I am just running it through the following page through the main.js

<!doctype html>
<html><head>
    <meta charset="utf-8">
    <title>Home - Dashboard</title>

    <script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
  <script type="text/javascript" src="main.js"></script>    
</head>
<body>
</body>
</html>

I have tried

dataType: 'json',

instead of jsonp and still get this issue. I added the crossdomain: true following another post on this site, but still receive the error.

where am I going wrong here?

my server logs the following in the console when I run the ajax

192.168.0.149 - - [2017-01-07 10:25:53] "GET /api/environment?seconds=10&callback=jQuery11110725321438557059_1483745153757&_=1483745153758 HTTP/1.1" 200 3977 0.510238

it logs this when I hit it through the browser directly.

192.168.0.149 - - [2017-01-07 10:27:02] "GET /api/environment?seconds=10 HTTP/1.1" 200 3977 0.629556

both my webserver and the server running the python service are located on the same network/subnet.

btw: the python services is the http://ouimeaux.readthedocs.io/en/latest/readme.html

thanks in advance

4
  • I think your server is responding with incomplete JSON. There is a missing }, which throws Uncaught SyntaxError: Unexpected token : Commented Jan 6, 2017 at 23:40
  • Is the full output json? Commented Jan 6, 2017 at 23:40
  • Sorry i did truncate the json for the post. it is a long list. When i pasted the json output into JSONlint it validated OK, so I think the json response is OK Commented Jan 7, 2017 at 0:01
  • "I have tried ... instead of jsonp and still get this issue" Are you saying you are getting the exact same error? I doubt that. Commented Jan 7, 2017 at 0:07

4 Answers 4

0

Your output is not a valid json, it should be either array of object or object. your output json is missing "}".

{
"Bedroom Light": {
    "host": "192.168.0.121", 
    "model": "Belkin Plugin Socket 1.0", 
    "name": "Bedroom Light", 
    "serialnumber": "221323K1300027", 
    "state": 0, 
    "type": "LightSwitch"}
}
Sign up to request clarification or add additional context in comments.

2 Comments

While that's correct, that's not the issue the OP has.
Sorry i did truncate the json for the post. it is a long list. When i pasted the json output into JSONlint it validated OK, so I think the json response is OK
0

You are telling jQuery to request JSONP:

dataType: 'jsonp',

but the server is sending back JSON, not JSONP. JSON is not valid JSONP, hence you get that error.

You can either

  • have to make the server send back JSONP, or
  • enable CORS (on the service) and tell jQuery to expect JSON:

    dataType: 'json',
    

6 Comments

he mentioned already tried with data type as json
@BalajiM: Right, but he probably didn't enable CORS.
thanks. yes when I use json. I receive this as the error. XMLHttpRequest cannot load 192.168.0.109:5000/api/environment. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.
@ScottS: Yep. That's what you get when CORS is not enabled.
I am running the ouimeaux on a raspberryPi. it is python server running in the console at the moment, not on any webserver. Doesn't CORS need to be enabled through a webserver. both my script and the python server are all on the same home network.
|
-1

JSON.stringify(data) before you pass it. This will turn it into the proper format for the request to handle it

Comments

-1

TRY this..

.ajax({
  method: "GET",
  url: "http://192.168.0.109:5000/api/environment?seconds=10",
  dataType: "jsonp"
}).done(function(data) {
      console.log('sucess',data);
      });

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.