简体   繁体   English

意外的标记 ':'。 解析错误。 在AJAX响应中

[英]Unexpected token ':'. Parse error. in AJAX response

My Ajax request code: 我的Ajax请求代码:

$(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. 但是,当它获取JSON时,控制台会写入错误的Unexpected token ':'. Parse error. Unexpected token ':'. Parse error. All I need is to simply show this json or it's content in a div. 我只需要简单地显示此json或div中的内容即可。

The response JSON is: 响应JSON为:

{
  "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. 您告诉jQuery您想要JSONP,但服务器返回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. 由于jQuery处理JSONP的方式,普通的JSON就像是javascript一样被对待,因此您得到的错误实际上是浏览器的javascript解释器引发的语法错误。

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

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