Jquery ajax POST url被更改并返回错误请求

时间:2014-07-05 13:45:29

标签: javascript jquery ajax spotify

我在我的文件中使用Spotify Playlist Creator中的函数向API发送POST请求以创建新的播放列表。当我运行“Spotify Playlist Creator”代码时,该功能正常工作,POST请求返回一个带有新播放列表ID的响应对象。但是从我的代码运行时,相同的POST请求返回“400:错误请求”。

其他GET请求(用户配置文件请求)工作正常(因此我认为授权流程成功且access_token有效。)

当我使用Chrome检查器时,我注意到的一件事是,在工作应用中,POST网址为Request URL:https://api.spotify.com/v1/users/121856107/playlists 在我的应用中,它是Request URL:https://api.spotify.com/v1/users/121856107/playlists?{%22name%22:%22test_playlist%22,%22public%22:false}

这里的url附加了数据元素。为什么会这样?有什么指针吗?

下面我粘贴了在两个不同站点中表现不同的功能。

更新:看来POST请求以某种方式转换为OPTIONS。快速搜索显示此行为可能与“同源策略”有关。 我如何让我的功能开始工作?

function createPlaylist(username, name, callback) {
    var url = 'https://api.spotify.com/v1/users/' + username +
        '/playlists';
    $.ajax(url, {
        method: 'POST',
        data: JSON.stringify({
            'name': name,
            'public': false
        }),
        dataType: 'json',
        headers: {
            'Authorization': 'Bearer ' + g_access_token,
            'Content-Type': 'application/json'
        },
        success: function(r) {
            console.log('create playlist response', r);
            callback(r.id);
        },
        error: function(r) {
            callback(null);
        }
    });
}

0 个答案:

没有答案