0

This probably has been answered elsewhere but I for the life of me can not get this to work.

Here is my html:

        <!Doctype html>
<html ng-app>
  <head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
     <script src="JS.js"></script>  
  </head>
  <body>
    <div>
    </div>
  </body>
</html>

and here is my Js file:

$( document ).ready(function() {
  console.log( 'ready!' );

$.ajax({
    url: 'http://steamcommunity.com/market/priceoverview/?appid=730&currency=3&market_hash_name=StatTrak%E2%84%A2%20M4A1-S%20|%20Hyper%20Beast%20(Minimal%20Wear)',
    type: 'GET',
    dataType: 'jsonp',
    success: function(data) {
        console.log(data);
        var lowestprice = $( 'lowest_price' );
        console.log($target);
    }
});
});

The result of the query is:

{"success":true,"lowest_price":"48,--\u20ac","volume":"12","median_price":"51,02\u20ac"}

but I get the error uncaught SyntaxError: Unexpected token :

Thank you in advance, I'm experimenting with web programming and am struggling cause I am more used to object oriented programming.

6
  • which line is that error reported from. Your JSON looks fine and I cant see what (in the code you posted) might throw that error. That your code contains console.log($target) and $target is not defined anywhere makes me think you're not showing all the relevant parts Commented May 8, 2017 at 16:12
  • look at this link, stackoverflow.com/questions/43821423/… Commented May 8, 2017 at 16:13
  • It usually tells you what line the problem is. the js looks fine Commented May 8, 2017 at 16:14
  • var lowestprice = $( 'lowest_price' ); will not work until . (class selector) or #(id selector) not used with 'lowest_price' Commented May 8, 2017 at 16:15
  • That response ain't JSONP. You can not just use JSONP to get around CORS, the api needs to support it. Commented May 8, 2017 at 16:15

2 Answers 2

1

You are saying the endpoint is JSONP when you make the Ajax Request. The api returns JSON so when it is injected into the page it generates the error you see.

You can not just make any call JSONP by setting it in the Ajax call. The API needs to support JSONP.

Check the API to see if it supports JSONP. If it does set whatever querystring argument it requires. If it does not support it, than you would need to use a serverside proxy to make the call.

Sign up to request clarification or add additional context in comments.

2 Comments

What api will support JSONP or how can I make this happen.
I do not know if that API supports it, you would need to read its documentation to see if it does.
0

First of all, its cross domain and the url should be modified to use HTTPS (i just tried it on w3school's editor. (otherwise, browser will block such url...)

Next, you need to use JSON instead of JSONP. After that, you will start getting status 200 ok. This is what i tried

   $.ajax({
        url: 'https://steamcommunity.com/market/priceoverview/?appid=730&currency=3&market_hash_name=StatTrak%E2%84%A2%20M4A1-S%20|%20Hyper%20Beast%20(Minimal%20Wear)',
        type: 'GET',
           dataType: 'json',
        success: function(data) {
            alert(data);
            var lowestprice = $( 'lowest_price' );
            console.log($target);
        },
           error: function(st, err) {
            alert('vijay');
            alert(st.status);
            alert(err);
           }
    });

Now, you have to add that cross-domain header...This page will tell you the explanation in detail.

https://www.html5rocks.com/en/tutorials/cors/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.