简体   繁体   English

使用Trakt.tv api中的jsonp从jQuery $ .ajax post return获取parseerror

[英]Getting a parseerror from jQuery $.ajax post return using jsonp from Trakt.tv api

Working with the Trakt.tv API. 使用Trakt.tv API。 It looks like I'm sending valid json as I'm able to authenticate but the return I receive is a parse error. 看来我可以验证身份时正在发送有效的json,但收到的返回消息是解析错误。

Resource interpreted as Script but transferred with MIME type text/html: 
http://api.trakt.tv/recommendations/shows/myApiKeyCompleteNumbers?callback=jQuery111000155555475132972_1397674204444&{%22username%22:%22userName%22,%22password%22:%22mySha1PassComplete%22}&_=1397674207093

Uncaught SyntaxError: Unexpected identifier 

The return says: 回报说:

Disallowed Key Characters.

I'm using: jQuery 1.11.0 我正在使用:jQuery 1.11.0

Thanks in advance for any help or guidance 在此先感谢您的帮助或指导

$(document).ready(function () {

    function success(data) {
        alert('data: ' + data);
    }

    var traktUser = 'myUserName';
    var traktHash = 'mySha1Password';
    var traktApi = 'myApiKey';
    var data = {
        'username': traktUser,
        'password': traktHash
    };
    var postData = JSON.stringify(data);
    var apiUrl = 'http://api.trakt.tv/recommendations/shows/' + traktApi;

    $.ajax({
        type: 'POST',
        url: apiUrl,
        data: postData,
        contentType: 'application/json',
        dataType: 'jsonp',
    }).
    done(success);

}); //document ready

You can't make a POST request using JSONP, jQuery is ignoring the POST instruction and making a GET request. 您无法使用JSONP发出POST请求,jQuery忽略了POST指令并发出GET请求。

Your data is being placed in the query string and is not properly URL Encoded. 您的数据被放置在查询字符串中,并且未正确进行URL编码。

The server is responding with an HTML document containing an error message instead of a JavaScript script formatted according to JSONP rules. 服务器正在响应包含错误消息的HTML文档,而不是根据JSONP规则格式化的JavaScript脚本。


It looks like the API you are trying to use does not support JSONP at all. 您要使用的API似乎根本不支持JSONP。 Since you are passing your own user credentials in the request, this makes sense. 由于您在请求中传递了自己的用户凭据,因此这很有意义。 JSONP is a hack to work around the Same Origin Policy that is implemented by browsers (these days we can use CORS instead) and there is no point in using it unless you want end user browsers to access the API directly. JSONP是一种可解决由浏览器实现的同源策略的黑客(如今我们可以改用CORS),除非您希望最终用户浏览器直接访问API,否则使用它毫无意义。 Since end user browsers couldn't access it without being given your username and password, it doesn't seem likely to be intended to be used that way. 由于最终用户浏览器无法在没有用户名和密码的情况下访问它,因此似乎不太可能以这种方式使用它。

Process the data from the API on your server instead. 而是从服务器上的API处理数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM