I'm writing a simple site that takes as input an idiom, and return its meaning(s) and example(s) from Oxford Dictionary. Here's my idea:
I send a request to the following URL:
http://www.oxfordlearnersdictionaries.com/search/english/direct/?q=[idiom]
For example, if the idiom is “not go far”, I'll send a request to:
http://www.oxfordlearnersdictionaries.com/search/english/direct/?q=not+go+far
And I'll be redirected to the following page:
http://www.oxfordlearnersdictionaries.com/definition/english/far_1#far_1__192
On this page, I can extract the meaning(s) and the example(s) of the idiom.
Here's my code for testing. It will alert the response URL:
<input id="idiom" type="text" name="" value="" placeholder="Enter your idiom here"> <br> <button id="submit" type="">Submit</button> <script type="text/javascript"> $(document).ready(function(){ $("#submit").bind('click',function(){ var idiom=$("#idiom").val(); $.ajax({ type: "GET", url: 'http://www.oxfordlearnersdictionaries.com/search/english/direct/', data:{q:idiom}, async:true, crossDomain:true, success: function(data, status, xhr) { alert(xhr.getResponseHeader('Location')); } }); }); }); </script>
The problem is I've got an error:
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://www.oxfordlearnersdictionaries.com/search/english/direct/?q=by+far. This can be fixed by moving the resource to the same domain or enabling CORS.
Can anybody tell me how to resolve this please?
Another approach is fine too.
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://www.oxfordlearnersdictionaries.com/search/english/direct/?q=by+far. This can be fixed by moving the resource to the same domain or enabling CORS.
CORS is a mechanism that defines a procedure in which the browser and the web server interact to determine whether to allow a web page to access a resource from different origin. Figure 2. Cross domain ajax request. When you do a cross-origin request, the browser sends Origin header with the current domain value.
CORS or Cross Origin Resource Sharing is blocked in modern browsers by default (in JavaScript APIs). Installing this add-on will allow you to unblock this feature. Please note that, when the add-on is added to your browser, it is in-active by default (toolbar icon is grey C letter).
JSONP or "JSON with padding" is a communication technique used in JavaScript programs running in web browsers to request data from a server in a different domain, something prohibited by typical web browsers because of the same-origin policy. JSONP takes advantage of the fact that browsers do not enforce the same-origin policy on script tags. Note that for JSONP to work, a server must know how to reply with JSONP-formatted results. JSONP does not work with JSON-formatted results.
http://en.wikipedia.org/wiki/JSONP
Good answer on StackOverflow: jQuery AJAX cross domain
$.ajax({ type: "GET", url: 'http://www.oxfordlearnersdictionaries.com/search/english/direct/', data:{q:idiom}, async:true, dataType : 'jsonp', //you may use jsonp for cross origin request crossDomain:true, success: function(data, status, xhr) { alert(xhr.getResponseHeader('Location')); } });
Place below line at the top of the file which you are calling through AJAX.
header("Access-Control-Allow-Origin: *");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With