4

オープンソース ライブラリ ( qwest.js )である goo.gl URL Shortener API を使用して URL を短縮しようとしています。jqueryを使用して成功しましたが、「このAPIはフォームエンコードされた入力の解析をサポートしていません」というエラーが表示されます。クエストを使用した場合。

jquery を使用した私のコード:

var longURL = "http://www.google.com/";
 $.ajax({
        url: 'https://www.googleapis.com/urlshortener/v1/url?key=AIzaSyANFw1rVq_vnIzT4vVOwIw3fF1qHXV7Mjw',
        type: 'POST',
        contentType: 'application/json; charset=utf-8', 
        data: '{ longUrl:"'+ longURL+'"}',         
        success: function(response) {
          console.log(response)
        }
 })
.done(function(res) {
    console.log("success"); 
})
.fail(function() {
    console.log("error");
})
.always(function() {
    console.log("complete");
});

および qwest.js を使用した動作しないコード

var longURL = "http://www.google.com/"    
qwest.post('https://www.googleapis.com/urlshortener/v1/url?key=479dfb502221d2b4c4a0433c600e16ba5dc0df4e&',
    {longUrl: longURL}, 
    {responseType:'application/json; charset=utf-8'})
                    .then(function(response) {
                        // Make some useful actions
                    })
                    .catch(function(e, url) {
                        // Process the error
                    });

どんな助けでも強くお勧めします。

4

1 に答える 1

6

qwest の作成者はこちら ;)

ドキュメントに記載されているとおり: the default Content-Type header is application/x-www-form-urlencoded for post and xhr2 data types, with a POST request.

ただし、Google Shortener サービスでは受け付けていません。JSON入力タイプが必要だと思います。次にdataType、qwest のオプションを に設定する必要がありますjson。さらに、あなたのresponseTypeオプションは無効であり、ドキュメントに従っていません。Content-Type通常、Google が有効なヘッダーでリクエストに応答する場合は、設定する必要はありません。良いコードは次のとおりです。

qwest.post('https://www.googleapis.com/urlshortener/v1/url?key=479dfb502221d2b4c4a0433c600e16ba5dc0df4e&', {longUrl: longURL}, {dataType:'json'})

Google が認識した を送信しない場合は、オプションをtooContent-Typeに設定してください。responseTypejson

于 2015-03-21T10:04:02.930 に答える