简体   繁体   English

JSONP AJAX请求中的“未捕获的语法错误:意外的令牌:”

[英]'Uncaught Syntax Error: Unexpected token :' in JSONP AJAX request

I already read many answers on similar questions like this, but didn't find the right solution that worked for me so I have to ask. 我已经阅读了类似此类问题的许多答案,但没有找到适合我的正确解决方案,因此我不得不提出疑问。 Here is my code : 这是我的代码:

$.ajax({
    url: 'http://beta.zelenaposta.sk/oauth/request-token',
    dataType: 'jsonp',
    type: 'POST',
    contentType: 'application/x-www-form-urlencoded',
    data: {
        oauth_consumer_key: some_value,
        oauth_signature: some_other_value,
        oauth_signature_method: 'PLAINTEXT'
    }
}).done(function(data) {
    console.log(data);
}).fail(function(jqXHR,errorThrown, data){
    console.log(errorThrown);
    console.log(jqXHR.responseText);
    console.log(jqXHR.status);
})

Here is how to get request token: 这是获取请求令牌的方法:

POST /oauth/request-token HTTP/1.1 
Host: zelenaposta.sk 
Content-Type: application/x-www-form-urlencoded 

oauth_consumer_key=some_value& 
oauth_signature=some_other_value& 
oauth_signature_method=PLAINTEXT

This request sends this response (if successful): 该请求发送以下响应(如果成功):

{
    "token": "some_new_value",
    "error": false,
    "errorMessage": null
}

And here is the error that i get : 这是我得到的错误:

errorThrown = parsererror errorThrown =解析器错误
jqXHR.responseText = null jqXHR.responseText = null
Uncaught SyntaxError: Unexpected token : 未捕获到的SyntaxError:意外令牌:

How can I get rid of these errors? 如何摆脱这些错误? I need to use JSONP because of cross site scripting and I don't have any access to the server where I'm posting the request. 由于跨站点脚本编写,我需要使用JSONP,但是我对发布请求的服务器没有任何访问权限。 I would really appreciate any kind of help. 我真的很感谢任何帮助。

I had the same problem too, it turned out there were some special characters that caused a parsing error: character/substrings like "/", "/n", "/t" could not be parsed. 我也遇到了同样的问题,结果是有一些特殊的字符导致了解析错误:无法解析诸如“ /”,“ / n”,“ / t”之类的字符/子字符串。 What I did was a replace in my WebMethod function before returning the JSON. 我所做的是在返回JSON之前在WebMethod函数中进行了替换。

        json = json.Replace("\\n", "\\\\n");
        json = json.Replace("\\t", "\\\\t");
        json = json.Replace("\\\"", "\\\\\"");

Note the number of backslashes in there :) I discovered what the problem was using Firefox and Firebug, the error had much more details than the one Chrome Console gave me. 请注意其中的反斜杠数量:)我发现了使用Firefox和Firebug的问题所在,该错误的详细信息比一个Chrome控制台提供的更多。 A very useful hint was the exact column where the parsing error occured, wich helped me locate the character. 一个非常有用的提示是解析错误发生的确切列,这帮助我找到了字符。

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

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