简体   繁体   中英

Unexpected token ':'. Parse error. in AJAX response

My Ajax request code:

$(window).ready(function () {
    var $form = $(document).find('#name-form');
    var $display = $(document).find('#display');
    $form.on('submit', function (e) {
        e.preventDefault();
        var name = $form.find('#name').val();
        var surname = $form.find('#surname').val();
        var patronymic = $form.find('#patronymic').val();
        var year = $form.find('#year').val();
        var request = $.ajax({
            headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
            },
            dataType: 'jsonp',
            method: 'get',
            jsonp: false,
            jsonpCallback: "localJsonpCallback",
            url: 'http://127.0.0.1:8885/search?app=potato',
            data : {
                name: name,
                surname: surname,
                patronymic: patronymic,
                year: year,
            }
        });
     function localJsonpCallback(json) {
            if (!json.Error) {
                $('#display').submit();
            }
            else {
                $('#display').show();
                alert(json.Message);
            }
        }
    });
});

This code sends the request, this is for sure. But when it gets the JSON, console writes an error Unexpected token ':'. Parse error. Unexpected token ':'. Parse error. All I need is to simply show this json or it's content in a div.

The response JSON is:

{
  "Report": "http://127.0.0.1:8099/chicken/eggs.html",
  "Exist": true
}

What is the problem?

You're telling jQuery that you want JSONP but the server is returning JSON.

Due to how jQuery handles JSONP, the normal JSON is treated as though it's javascript and so the error you're getting is actually a syntax error thrown by the browser's javascript interpreter.

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