简体   繁体   English

Jquery Ajax - 未捕获的SyntaxError - 意外的令牌

[英]Jquery Ajax - Uncaught SyntaxError - Unexpected token

I am trying to make an AJAX jsonp request to a temporary source of data for testing. 我正在尝试向临时数据源发出AJAX jsonp请求以进行测试。 This is the code I am using: 这是我正在使用的代码:

$('#search').click(function (event) {
    $.ajax({
        url: "http://api.test.com/v1/",
        type: "GET",
        contentType: "application/json",
        dataType: "jsonp",
        data: {q:$("#keyword").val()},
        timeout: 5000,
        beforeSend: function () {
            $('#content').fadeTo(500, 0.5);
        },
        success: function (data, textStatus) {
            $('html, body').animate({
                scrollTop: '0px'
            }, 300);
        $('#content').html(data.objects[0].category+'<br>'+data.objects[0].company);
        },
        error: function (x, t, m) {
            if (t === "timeout") {
                alert("Request timeout");
            } else {
                alert('Request error');
            }
        },
        complete: function () {
            $('#content').fadeTo(500, 1);
        }
    });
});

When ever I try and run the command I get the following error: 当我尝试运行该命令时,我收到以下错误:

Uncaught SyntaxError: Unexpected token : 未捕获的SyntaxError:意外的令牌:

I am new to jquery and ajax generally, and this has me stumped. 我一般都是jquery和ajax的新手,这让我很难过。 Where have I gone wrong? 我哪里出错了?

That URL that you're using does not appear to be a JSONP service. 您正在使用的URL似乎不是JSONP服务。 It's returning a plain JSON structure, and that won't work for JSONP. 它返回一个普通的JSON结构,这对JSONP不起作用。

You'll either need to figure out an alternative API for that service that is JSONP, or else query it from your server. 您需要为该服务找出一个替代API,即JSONP,或者从您的服务器查询它。

如果您使用JSONP作为数据类型,它从服务器返回应该是这样的

jQuery111207659580435138196_1433210804043({"status" : "success"})

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

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