簡體   English   中英

AJAX發布錯誤-SyntaxError:JSON位置5出現意外的令牌R

[英]AJAX Post Error - SyntaxError: Unexpected token R in JSON at position 5

我正在嘗試將以下JSON文件發送到AJAX POST方法。

請參閱下面的AJAX方法:-

$(document).on('submit', '#create-recipe-form', function(){
    // get form data
    var form_data= {
                     "recipe_name":"ghjghjh",
                     "category_name":"Desert",
                     "category_id":"8",
                     "Apple":"Apple",
                     "Carrots":"Carrots",
                     "step1":"ghj",
                     "step2":"",
                     "step3":"",
                     "step4":"",
                     "prep":"6"
                      };

    // submit form data to api
    $.ajax({
        url: "http://localhost:8082/recipe_app/api/recipes/create_recipe.php",
        type : "POST",
        //contentType : 'application/json',
        contentType : 'json',
        data : form_data,
        success : function(result) {

            createRecipeIngredientsForm();

            // recipe was created, go back to recipes list
            //showRecipes();
        },
        error: function(xhr, resp, text) {
            // show error to console
            console.log(xhr, resp, text);
        }
    });

上面的AJAX方法實際上正確地調用了.php文件,並且數據已插入數據庫中。 但是返回了錯誤,因此“成功”部分永遠不會運行。 這太令人沮喪了,有人可以照亮嗎?

SyntaxError:ajaxConvert( http:// localhost:8082 / recipe_app / app / assets / js / jquery-3.2.1.js:8754:19 )在parse()處位置5處的JSON中意外的令牌R( http: //Http://localhost:8082/recipe_app/app/assets/js/jquery-3.2.1.js:9222:15 )。 http:// localhost:8082 / recipe_app / app / assets / js / jquery-3.2.1.js:9514:9

替換此行的ajax

form_data=JSON.stringify($(this).serializeObject());

form_data=$(this).serializeObject();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM