【问题标题】:How to send a POST request with Advanced Custom Fields data如何使用高级自定义字段数据发送 POST 请求
【发布时间】:2020-09-18 16:33:53
【问题描述】:

我正在尝试使用一些自定义字段向其余 api 发送发布请求。这是我的代码

                let newCharacter = {
                    'title': $('.create-char-name').val(),
                    'acf': {
                        'char_class': $('#char-class').val(),
                        'char_subclass': $('#char-subclass').val(),
                        'char_level': $('#char-level').val()
                    },
                    'status': 'publish'
                }

                $.ajax({
                    beforeSend: (xhr) => {
                        xhr.setRequestHeader('X-WP-Nonce', spbk_data.nonce);
                    },
                    url: spbk_data.root_url + '/wp-json/wp/v2/character/',
                    method: 'POST',
                    data: newCharacter,
                    success: (response) => {
                        console.log("congrats");
                        console.log(response);
                    },
                    error: (response) => {
                        console.log("Sorry");
                        console.log(response);
                    }
                });

请求通过没有任何问题,但是当我检查 json 时,“acf”字段返回 false。 如果该信息有用,我正在使用acf to wp api plugin

我发现的关于这个问题的唯一信息是this post,但我不太明白答案的含义。我尝试在随机数下方添加xhr.setRequestHeader('Content-Type', application/json);(我也尝试使用小写首字母),就像帖子似乎暗示的那样,但这会返回此错误:

"{"code":"rest_invalid_json","message":"Invalid JSON body passed.","data":{"status":400,"json_error_code":4,"json_error_message":"Syntax error"}}"

【问题讨论】:

    标签: javascript wordpress advanced-custom-fields


    【解决方案1】:

    试试下面的方法:

    function NewCharacter(){
    
        this.title;
        this.acf;
        this.status;
    }
    
    function CharInfo(){
    
        this.char_class;
        this.char_subclass;
        this.char_level;
    
    }       
    
    var charInfo = new CharInfo();
    charInfo.char_class=$('#char-class').val();
    charInfo.char_subclass=$('#char-subclass').val();
    charInfo.char_level=$('#char-level').val();
    
    var newCharacter = new NewCharacter();
    newCharacter.title=$('.create-char-name').val();
    newCharacter.acf=charInfo
    newCharacter.status="publish";
    
    $.ajax({
         beforeSend: (xhr) => {
             xhr.setRequestHeader('X-WP-Nonce', spbk_data.nonce);
         },
         url: spbk_data.root_url + '/wp-json/wp/v2/character/',
         method: 'POST',
         data: JSON.stringify(newCharacter),
         success: (response) => {
             console.log("congrats");
             console.log(response);
         },
         error: (response) => {
             console.log("Sorry");
             console.log(response);
         }
    });
    
    

    【讨论】:

    • 或者,尝试在您的解决方案中直接使用 JSON.stringify(newCharacter)。您应该传递 JSON 字符串,而不是 Object 本身
    • 那行不通。我尝试了您和我的解决方案,但它们返回一个空对象
    【解决方案2】:

    是的,我有点笨。我尝试another plugin 在 acf 和其他 api 之间架起一座桥梁,并且成功了。

    我多次想到尝试另一个插件,但我认为“他们做同样的事情,尝试没有意义”。这表明您不应该对看起来太明显或太愚蠢的解决方案置之不理。

    【讨论】:

      猜你喜欢
      • 2013-11-19
      • 1970-01-01
      • 2015-01-18
      • 1970-01-01
      • 2013-11-25
      • 1970-01-01
      • 1970-01-01
      • 2021-06-08
      • 2017-12-16
      相关资源
      最近更新 更多