简体   繁体   English

成功功能未被执行

[英]success function not being executed

I am making a ajax call to get a record from the database I have seen in the console that it is successfully returning the record but success is function is not being executed. 我正在进行一次ajax调用,从我在控制台中看到的数据库中获取一条记录,表明它已成功返回记录,但成功是函数未被执行。

function ajaxForParent() {
    var search = document.getElementById("resourceId").value;
    var folderId = document.getElementById("folderId").value;

    $.ajax({
        type : "GET",
        contentType : 'application/json; charset=utf-8',
        dataType : 'json',
        url : "/admin/content/changeParent",
        data : {search : search, folderId : folderId}, // Note it is important
        success : function(folder) {
            alert(folder);
            if (folder.id == document.getElementById("folderId").value) {
                $("#parentAvailable").html("Children Limit exceeding you cannot add more than 9 levels children");
                return false;
            }

            console.log(folder.id);
            console.log(document.getElementById("folderId").value);

            if (folder.id != document.getElementById("folderId").value) {
                console.log("I am coming here " +folder.id);
                var s = document.getElementById("folderId");
                s.value = folder.id;
            }
            $("#parentAvailable").html("Parent Found Successfully!");
        },
        error : function(content) {
            $("#parentAvailable").html("Parent Not available");
        }
    });
}

I have tried with the above it always entering the error function even though record is successfully return from server side. 我已经尝试过上面的它总是进入错误功能,即使记录从服务器端成功返回。 can anyone tell me what I am doing wrong here ? 谁能告诉我这里我做错了什么?

when I throw the error I am getting this 当我抛出错误时,我得到了这个

SyntaxError: JSON.parse: end of data after property value in object at line 1 column 1587309 of the JSON data

You cannot send json data via GET . 您无法通过GET发送json数据。 You need to use POST : 你需要使用POST

$.ajax({
    type : "POST", // <------ Use POST 
    contentType : 'application/json; charset=utf-8',
    dataType : 'json',
    url : "/admin/content/changeParent",
    data : {search : search, folderId : folderId},

If your app /admin/content/changeParent accepts only GET, you may need to consider changing it to accept POST. 如果您的app /admin/content/changeParent仅接受GET,您可能需要考虑将其更改为接受POST。 Otherwise, your app cannot receive the JSON data you send to the server correctly via GET. 否则,您的应用无法通过GET正确接收您发送到服务器的JSON数据。

It looks like the response contains improperly formatted json data. 看起来响应包含格式不正确的json数据。 Try copying the response into a json linter such as http://jsonlint.com/ to find the problem. 尝试将响应复制到json linter(例如http://jsonlint.com/)以查找问题。

take your server answer and paste it into some json linter (like http://jsonlint.com/ ) to make sure jquery can transform it to a valid response. 把你的服务器答案并粘贴到一些json linter(如http://jsonlint.com/ ),以确保jquery可以将其转换为有效的响应。

you can also put a breakpoint into your error callback and check for the error and value returned at that moment. 您还可以在错误回调中添加断点,并检查当时返回的错误和值。 While you're there, if the data received is a string and not an object, try to call JSON.parse(/* your server response */) to test if it's working at all and if you get any additional error. 当你在那里时,如果收到的数据是一个字符串而不是一个对象,尝试调用JSON.parse(/ *你的服务器响应* /)来测试它是否正常工作以及是否有任何其他错误。

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

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