Je rencontre actuellement des problèmes pour raccourcir une URL avec le raccourci Google URL. J'utilise CoffeeScript mais le code généré semble bon. Voici ce que je fais:Google URL Shortener Bad Request avec jQuery
shortenUrl = (longUrl) ->
$.ajax(
type: 'POST'
url: "https://www.googleapis.com/urlshortener/v1/url?key=myAPIkey"
data:
longUrl: longUrl
dataType: 'json'
success: (response) ->
console.log response.data
contentType: 'application/json'
);
Le code généré est:
shortenUrl = function(longUrl) {
return $.ajax(console.log({
longUrl: longUrl
}), {
type: 'POST',
url: "https://www.googleapis.com/urlshortener/v1/url?key=myAPIkey",
data: {
longUrl: longUrl
},
dataType: 'json',
success: function(response) {
return console.log(response.data);
},
contentType: 'application/json'
});
};
Voici l'erreur que je reçois dans la console JS Chrome:
POST https://www.googleapis.com/urlshortener/v1/url?key=myAPIkey 400 (Bad Request)
(précisément, il y a apparemment une erreur Parse)
Notez que, quand j'exécute une demande boucle comme ceci:
curl https://www.googleapis.com/urlshortener/v1/url?key=myAPIkey \
-H 'Content-Type: application/json' \
-d '{longUrl: "http://www.google.com/"}'
Cela fonctionne comme un charme. Et je reçois:
{
"kind": "urlshortener#url",
"id": "http://goo.gl/fbsS",
"longUrl": "http://www.google.com/"
}
Alors, quel est le problème avec ce jQuery? (J'utilise 1.9.x version)
EDIT: Voici la bonne façon de le faire avec jQuery:
shortenUrl = function(longUrl) {
return $.ajax(console.log({
longUrl: longUrl
}), {
type: 'POST',
url: "https://www.googleapis.com/urlshortener/v1/url?key=myAPIkey",
data: '{ longUrl: longUrl }', // <-- string here
dataType: 'json',
success: function(response) {
return console.log(response.data);
},
contentType: 'application/json'
});
};
Face à même question en rubis, s'il vous plaît regarder le lien ci-dessous, http://stackoverflow.com/a/23562091/344993 – Parth