Reputation: 49
Hi I am trying to fetch values from Envato using jQuery.ajax()
, the URL parameter contains a colon which is not accepted by system.
$('button').click(function(){
var request=$.ajax(
url: "https://api.envato.com/v1/market/random-new-files:themeforest.json",
type: "GET",
connection: "keep-alive",
contentType: "application/json",
mimeType: "application/json ",
headers: {
"Authorization": 'Bearer uO5xtTUXgJMyYPKg5fC9NNRGcue42QeD'
}
)}
request.done(function (msg) {
console.log(msg);
})
This is the call, but I am getting error as per following screenshots: http://prntscr.com/b7poyo http://prntscr.com/b7pp1b
Upvotes: 0
Views: 1240
Reputation: 11
The syntax error, Unexpected token :
, is because JSONP is parsed as JavaScript, where {...}
also represents blocks.
var callback = req.query.callback;
var data = JSON.stringify({
Name : "Tom",
Description : "Hello it's me!"
});
if (callback) {
res.setHeader('Content-Type', 'text/javascript');
res.end(callback + '(' + data + ')');
} else {
res.setHeader('Content-Type', 'application/json');
res.end(data);
}
xpressJS also includes res.jsonp()
that already implements this condition:
app.get( '/', function( req, res ) {
console.log( 'req received' );
res.jsonp({
Name : "Tom",
Description : "Hello it's me!"
});
});
Upvotes: 0
Reputation: 32354
You have some sintax errors
Try the following:
$('button').click(function() {
var request = $.ajax({
url: "https://api.envato.com/v1/market/random-new-files:themeforest.json",
type: "GET",
connection: "keep-alive",
contentType: "application/json",
mimeType: "application/json ",
headers: {
"Authorization": 'Bearer uO5xtTUXgJMyYPKg5fC9NNRGcue42QeD'
},
success: function(data) {
console.log(data)
}
})
});
https://jsfiddle.net/2ukdh27r/
Upvotes: 1
Reputation: 4435
I had this answer typed up, mostly finished by the time I noticed madalin's answer. It's basically the same code, but forgive me as I like to try and explain my answer a little.
First thing first: I have no idea what you did. Your Javascript in your question just immediately rang out to me as wrong. You had a lot of missing parenthesis and brackets. So, I started off by just making a very basic ajax call, e.g.
$.ajax({
url: "https://api.envato.com/v1/market/random-new-files:themeforest.json"
}).always(function(r) {
console.log(r)
});
I got an authentication error. I just copied over the rest of your .ajax()
object and it worked just fine.
Lesson for Next Time Be careful about matching parenthesis and brackets.
$('button').click(function() {
$.ajax({
url: 'https://api.envato.com/v1/market/random-new-files:themeforest.json',
type: "GET",
connection: "keep-alive",
contentType: "application/json",
mimeType: "application/json ",
headers: {
"Authorization": 'Bearer uO5xtTUXgJMyYPKg5fC9NNRGcue42QeD'
}
}).done(function(r) {
console.log(r);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button>Click me</button>
Upvotes: 0
Reputation: 312
Try to change the URL as follows:
url: "https://api.envato.com/v1/market/"+encodeURIComponent("random-new-files:themeforest.json")
Upvotes: 0