0

I'm trying to use a certain service to check for proxies. They have a simple API from which they return JSON. I'd like to get this JSON on my server.

No matter what I do, I either get a CORS request problem or a SyntaxError: missing ; before statement message.

Here's my code:

<h1>Test Page</h1>
<a href="#" onclick="checkProxy()">Test Button</a>


<script>
function checkProxy() {
  console.log("Checking...");
  $.ajax({
    url: 'http://api.ip2proxy.com/?ip=105.159.246.30&key=demo',
    dataType: 'jsonp',
    success: function(data){
      console.log(data);
    }
  });
}

Clicking my 'button' calls the funtion, which returns the aforementioned syntax error. Changing the datatype to JSON gives me a CORS error.

The strange thing is, when I use a different URL -- example that I found in another StackOverflow thread: http://www.locationbox.com.tr/locationbox/services?Key=3430000202000191008400080609030X20201090060050260003069&Cmd=IlList -- it logs the data just fine.

1
  • Then maybe the data isn't meant to be shared publicly. Have you checked the API of the data source? Commented Sep 9, 2016 at 17:13

1 Answer 1

1

I'd like to get this JSON on my server.

You'll need to write code that runs on the server to do that. The reason your code is failing is that you're running it on a browser, where the Same Origin Policy comes into play: Your page's origin isn't granted access by that API endpoint via CORS. Unless they allow you access, or they provide a JSONP endpoint you can use instead, you cannot directly query it from a browser.

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

5 Comments

So I have to fetch the JSON using PHP?
@DanielJ: No, but you have to fetch it on a server. You can do that with PHP, Python, JavaScript (in NodeJS, in a JVM), Java, C# or VB .Net, ... :-)
Ah, right. Sometimes I forget that server side doesn't always mean PHP! It does in my case, though. :) Thank you very much! This has solved my problem. I'm able to get data now.
@t-j-crowder: So why is it that one example URL I provided gives a syntax error, and the other works? They're both valid JSON.
@DanielJ: JSONP != JSON. Here's an example of a valid JSONP response: callback({"foo":"bar"}). Here's an example of a valid JSON response: {"foo":"bar"} Your example ajax call tells jQuery it's using JSONP. Presumably one of your URLs is, the other isn't.

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.