JavaScript REST Request without AJAX (omit JSONP)? - javascript

I'm using JSONP to send a REST Request, and I can see the response (json) in the chrome dev tools but can't use the result because I'm getting the error "unexpected token :". As you can read in linked question, this is because a function is expected as response (i get json).
Because I can't modify the webservice I need to access the webservice without AJAX so I dont have those problems with "same origin policy" and can use normal json as the data type. Every Example I can find is using AJAX. Isn't there another possibility?

Related

Getting OG data impossible because of CORB

I'm trying to scrape OG data using open-graph-scraper and Vue.js, but it gets blocked because of CORB in Chrome. It is working fine for scripts I run using Node, but is there a work around for this problem or another to get OG data from an input URL?
Cross-Origin Read Blocking (CORB) blocked cross-origin response SOME-INPUT-URL with MIME type text/html. See https://www.chromestatus.com/feature/5629709824032768 for more details.
CORB is a standard that is based around the idea
"what I asked for is what I received"
if you are troubleshooting CORB errors in chrome it means that your request method is being responded to with either no content type or a content type that does not correspond to your method of request.
in a 3rd party API what I would recommend is using a tool like postman (unaffiliated) to inspect the incoming content type in the response headers. Once you know the content-type header of the call then tweak your request method in an attempt to match the expected output. Many API's use poorly implemented frameworks so just because the response is well formed JSON doesn't mean that the header will say JSON. I often see json outputs where the headers are 'text/plain'.
If this doesn't work, for example some api's fail to respond with content headers at all
- another method would be to use an intermediary call to make the request. For example using a combination of the AWS API gateway and a AWS Lambda function you can create a route that will make the request using node but where you then gain the total control over both the response headers and body. You can then add the content header you desire and pass it back to your client.
ref:
https://www.chromium.org/Home/chromium-security/corb-for-developers

Why won't $.getJson work with json or jsonp?

I am trying to pull information from Google Finances stock screener. There is no official api, so I am just making a get request to the same URL that they use on the site. I am using the URL at the bottom of the question, it can get a bit long. I can go to the url myself and it will give me a text file with the JSON information. In my javascript I am using $.getJSON on the url to get the screener results. But I get a Access-Control-Allow-Origin error, so I change output=json to output=jsonp&callback=?. But it only returns ();. From what I can tell this means that it is not set up on the other end to respond to a jsonp request and cannot return the proper information.
I have also tried output=json&callback=?, which produces the (); and output=json&callback=callbackFunction and output=json&callback=callbackFunction which both give me Access-Control-Allow-Origin.
Is there any way that I can make this work?
https://www.google.com/finance?output=json&start=0&num=20&noIL=1&q=[currency%20%3D%3D%20%22USD%22%20%26%20%28%28exchange%20%3D%3D%20%22OTCMKTS%22%29%20%7C%20%28exchange%20%3D%3D%20%22OTCBB%22%29%20%7C%20%28exchange%20%3D%3D%20%22NYSEMKT%22%29%20%7C%20%28exchange%20%3D%3D%20%22NYSEARCA%22%29%20%7C%20%28exchange%20%3D%3D%20%22NYSE%22%29%20%7C%20%28exchange%20%3D%3D%20%22NASDAQ%22%29%29%20%26%20%28market_cap%20%3E%3D%200%29%20%26%20%28market_cap%20%3C%3D%200.1%29]&restype=company&ei=GLyhVKmcDpOb8gbm7IGQAQ
If the service doesn't provide a JSONP endpoint or use CORS to grant you permission to access some other kind of endpoint, then there is no way to access the data using client side code.
Use server side code instead. You can use that to present the data to your client side code.

Forcing a JSONP call to convert text response to JSON

If I've got the following request
$.ajax({
type: "GET",
dataType: "jsonp",
jsonp: "callback",
jsonpCallback: "my_callback",
url: my_https_url,
headers:{"Content-Type":"text/html; charset=utf-8"},
success: function(data) {
eval("json="+data);
}
error: function() {
console.log("fail");
}
});
I'm getting a server response with the 200 status code. The response header says it is sending back text/html as the content-type. However, I know the content is actually formatted JSON data. The problem is the server is not configured to accept JSONP requests (and I can't change it). I am using the JSONP approach for cross-domain-related reasons. The error status I get back is a "parseerror" because it's not recognized as JSON. Is there a way to override the function responsible for parsing the data so that I can force it to be treated as JSON? Possibly using eval()?
If your server is not capable of creating a JSONP formatted response and you have to use JSONP because of same-origin restrictions, then you're just plain out of luck. A JSONP formatted response is in the form of a executable script that calls the specified callback.
You generally can't fetch JSON from a different origin. You either need JSONP or you need a modern browser with a server configured to allow cross origin requests. If you can't configure the server to allow cross origin requests, then you need the server to create JSONP. If it won't do that, then you can't get the data from it. JSONP is a work-around for cross-origin requests and it REQUIRES server cooperation to create a JSONP formatted structure for the response.
If you are getting JSONP structure from the server and jQuery is getting a parse error when trying to parse the response as JSON, then that's because whatever the server is sending is not legal JSON inside of the JSONP structure. The fact that you're getting a parse error would mean that jQuery is trying to parse it as JSON and is getting an error when trying to do so. If that's the case, then you have two choices:
Fix the data the server is sending to be 100% legal JSON.
Accept the data as text and fix it to be legal JSON or parse it yourself in some manner that accepts it the way it is.
In either case, the first step is probably to console.log() the text that is being sent and understand exactly why it's not being accepted as legal JSON. Then, you'll better know what your options are. If you want help with understanding that, then add the exact server response to your question.

How to load a .json file from another website?

I need to get a json file from a website: "http://fipeapi.appspot.com/api/1/carros/marcas.json" that doesn't implement JSONP callback function, it only returns several errors.
I've tried to get using a JSON call and returns error "200 OK" because of the crossdomain issue, i've already tried JSONP too, but it returns "parsererror" because the remote server doesn't wrap the response on a callback function.
There's some way to get this content or it's impossible?
As suggested by Musa and Kevin B, you can use a proxy server script that will wrap the actual source you're trying to get in JSONP or CORS. For example JSONProxy allows you to request
http://jsonp.jit.su/?callback=myCallback&url=http://fipeapi.appspot.com/api/1/carros/marcas.json
Which will return the data wrapped in a call to myCallback(). The site also supports CORS if you prefer that, there's some examples in the link I provided above.

Chrome cannot parse single line JSONP object

I'm having issues getting this JSON object to load via JSONP in Chrome 18 (latest stable). For some reason it presents me the error: Uncaught SyntaxError: Unexpected token :. According to JSONLint, the response is a valid JSON object.
Here is the code example: http://jsfiddle.net/jakebellacera/2j7DL/1/
To add: I'm attempting to get around cross-domain, I have permissions to both servers, but we cannot allow cross-domain on a specific domain for specific reasons. If there is a better way to do this, please let me know.
The server at lpunderground.com is returning JSON, not JSONP.
What's special about JSONP is that it's a valid javascript statement - the data returned from the server is wrapped in a function call (or sometimes a variable assignment) and when returned it is evaluated as normal javascript. Typically the client passes the server in the request parameters a function name to use in wrapping the data.
See the Wikipedia article and the jQuery docs for the details.

Categories

Resources