My problem with CORS, Cross origin, we search from google and found solution with JSONP, but we recieve error while calling API throught JSONP, below mycode.
$.ajax({
type: 'POST',
url: 'myapi',
data: {login:login,key:key,action:action, format:'jsonp'},
dataType: "jsonp",
jsonpCallback: "jsonp_callback",
crossDomain: true,
success : function(response){
alert(response);
},
error: function(result) {
alert('error');
}
});
function jsonp_callback(json){
alert(json);
}
Uncaught SyntaxError: Unexpected identifier
please help me and thanks in advance.
Related
The code is in coffeescript
I am trying to read a json file using the code below (cross domain)
$.ajax
url: 'https://canttypecompanyurlhere/books.json'
dataType: 'JSONP'
jsonpCallback: 'callback'
type: 'GET'
success: (data) ->
console.log (data)
error: () ->
console.log('error')
And here's the json
{
"name": "book-1",
"author": "someone smart"
}
What am I doing wrong? I cannot get past the error "uncaught SyntaxError: Unexpected token :"
Please help
You're missing both the brackets to contain the $.ajax function parameters, as well as the braces to indicate the beginning and end of the data object, not to mention the commas at the end of each line of the object's definition. It should look like this:
$.ajax ({
url: 'https://canttypecompanyurlhere/books.json',
dataType: 'JSONP',
jsonpCallback: 'callback',
type: 'GET',
success: (data) -> {
console.log (data);
},
error: () -> {
console.log('error');
}
});
I used laravel5.2 in my project;
javascript:
$.ajax({
type: 'get',
url: 'http://XX.XX.XX.XX/account/status?callback=?',
dataType : 'jsonp',
contentType: "application/json",
jsonp : "callback",
jsonpCallback: "jsonpcallback",
success: function(data){
console.log(data);
},
error: function(){
alert('500 error!')
}
});
route:
Route::get('/account/status', 'HomeController#accountStatus');
controller:
public function accountStatus(Request $request)
{
return Response::json(array('name' => 'Steve', 'state' => 'CA'))->setCallback($request->input('callback'));
}
but the response information has the whole html file, it turns out my route in route.php doesn't work,because whatever I changed the request url, it returns the same error and shows:
SyntaxError: expected expression, got '<'
I am trying to get the data from say: http://steamcommunity.com/market/priceoverview/?appid=730¤cy=3&market_hash_name=StatTrak%E2%84%A2%20M4A1-S%20|%20Hyper%20Beast%20(Minimal%20Wear)
Which contains something like this:
{"success":true,"lowest_price":"65,84\u20ac","volume":"21","median_price":"65,19\u20ac"}
My code so far:
$.ajax({
url: "http://steamcommunity.com/market/priceoverview/?appid=730¤cy=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);
},
});
But I am getting the error Uncaught SyntaxError: Unexpected token :
<script>
(function(){
var searchURL = 'http://en.wiktionary.org/wiki/search';
$.ajax({
type: "GET",
url: searchURL,
dataType: "jsonp",
cache: false,
async:false,
success: function(responseData, textStatus, XMLHttpRequest){
iframe(responseData);
}
});
})();
</script>
I added this script to my html file and it is show the following errors, copy pasting the function in console is also showing the same errors.
Uncaught SyntaxError: Unexpected token <
Resource interpreted as Script but transferred with MIME type text/html
could anyone help me resolve this issue, I am using Chrome brower.
You can't request arbitrary pages via AJAX, and jsonp doesn't magically make that work. You need to use the Wiktionary API.
The URL is http://en.wiktionary.org/w/api.php.
$.ajax({
url: 'http://en.wiktionary.org/w/api.php',
dataType: 'jsonp', // will automatically add "?callback=jqueryXXX"
cache: true, // the API complains about the extra parameter
data: { // the parameters to add to the request
format: 'json',
action: 'query',
titles: 'test'
},
success: function(data){
console.log(data);
}
});
I get the following error when I try to get info from the mtgox API. In chrome I can see that using jsonp is working, and there's no cross domain errors:
$.ajax({
type: 'GET',
url: 'https://mtgox.com/api/1/BTCUSD/ticker?callback=?',
async: false,
contentType: 'application/json',
dataType: 'jsonp',
success: function(json) {
console.log('ok');
}
});