Javascript jquery Uncaught SyntaxError: Unexpected token :

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3551713/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 05:08:13  来源:igfitidea点击:

jquery Uncaught SyntaxError: Unexpected token :

javascriptjqueryajaxgoogle-chrome

提问by JSNewbie

my post looks like this:

我的帖子是这样的:

$.post('/ajaxvalidate/1', {"nid": nid}, function(data) {

I get the Uncaught SyntaxError: Unexpected token :error in google chrome.

Uncaught SyntaxError: Unexpected token :在谷歌浏览器中收到错误消息。

I understand that I need to set a content type or something, from this question: Uncaught SyntaxError: Unexpected token :

我知道我需要设置内容类型或其他内容,来自这个问题: Uncaught SyntaxError: Unexpected token :

I don't understand how to do it.

我不明白该怎么做。

http://api.jquery.com/jQuery.post/has no examples. :( Thanks.

http://api.jquery.com/jQuery.post/没有示例。:( 谢谢。

回答by David Hoerster

There are a number of examples on the jQuery.post API page that you linked to. They're about 1/3 of the way down the page. The last example on the page shows passing the 'json' content type in this method.

您链接到的 jQuery.post API 页面上有许多示例。它们大约在页面下方的 1/3 处。页面上的最后一个示例显示在此方法中传递“json”内容类型。

Here's a sample of doing a jQuery post-- and this works in jsFiddlewithout issue:

这是执行 jQuery 的示例post——这在jsFiddle 中没有问题:

$(function() {
    $.post("/ajax_json_echo/", {html: "stuff"}, function(data) { alert(data.post_response.html);}, "json");
});

I'm passing my data to the service using 'json' as my data type.

我正在使用“json”作为我的数据类型将我的数据传递给服务。

So, are you sure there isn't a server side error happening. Try running your call through Fiddler or Firebug and see what you get back. Also, what type of data is your service expecting?

那么,您确定没有发生服务器端错误。尝试通过 Fiddler 或 Firebug 运行您的呼叫,看看您能得到什么。此外,您的服务需要什么类型的数据?

Update your post with what you see and I'll update my answer accordingly.

用您看到的内容更新您的帖子,我会相应地更新我的答案。

Hope this helps.

希望这可以帮助。

回答by JSNewbie

I got the same error when I forget to close normally $(document).ready(function(){with );(not only })

当我忘记$(document).ready(function(){);(不仅})正常关闭时,我遇到了同样的错误