1

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?

0

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

Comments

0

If you are using JSONP as datatype, its return from the server should be something like this

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.