简体   繁体   中英

Jquery Ajax - Uncaught SyntaxError - Unexpected token

I am trying to make an AJAX jsonp request to a temporary source of data for testing. 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 :

I am new to jquery and ajax generally, and this has me stumped. Where have I gone wrong?

That URL that you're using does not appear to be a JSONP service. It's returning a plain JSON structure, and that won't work for JSONP.

You'll either need to figure out an alternative API for that service that is JSONP, or else query it from your server.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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