【问题标题】:Schema describes boolean, string found instead when I create list through Mailchimp API架构描述了布尔值,当我通过 Mailchimp API 创建列表时找到的字符串
【发布时间】:2017-04-29 07:19:59
【问题描述】:

我是开发 Mailchimp 应用程序的新手。 我正在尝试通过 Mailchimp API 创建列表。 有人对这个错误有同样的问题吗?

这是我用于复选框输入的 HTML 代码:

<input id="email_type_option" type="checkbox" name="email_type_option" value="true">
<input id="email_type_optionHidden" type="hidden" name="email_type_option" value="false">

这里是保存函数的脚本: 我使用了这个条件:

if (document.getElementById('email_type_option').checked) {
    document.getElementById('email_type_optionHidden').disabled = true;
} else if(document.getElementById('email_type_option')) {
    document.getElementById('email_type_optionHidden').disabled = false;
}

API 的响应是:

"{"type":"http://developer.mailchimp.com/…/mai…/guides/error-glossary/","title":"Invalid Resource","status":400,"detail":"The resource submitted could not be validated. For field-specific details, see the 'errors' array.","instance":"","errors":[{"field":"email_type_option","message":"Schema describes boolean, string found instead"}]}"

我试图在我的复选框输入中输入布尔值,但它总是返回那个错误。

如果我在浏览器上 console.log 显示正确的布尔值,但我不知道为什么我发送它时仍然收到该错误。

我想让这个值返回布尔值,而不是错误所说的字符串。 如何解决?

【问题讨论】:

    标签: php ajax laravel checkbox mailchimp


    【解决方案1】:

    我终于解决了这个问题。我使用 ajax 将它发送到服务器或控制器(我使用 Laravel PHP 框架),实际上我发送的内容被转换为字符串(包括数字或布尔值),因为我在想通过 Mailchimp API 发送它时使用了 json_encode。
    所以我在 PHP 中使用了filter_var 函数来保持我的布尔值不被转换为字符串。

    这是我的 ajax 请求:

    save = function()
        {
            // if checkbox is checked send true, if not checked send false.
            if (document.getElementById('email_type_option').checked) {
                document.getElementById('email_type_optionHidden').disabled = true;
            } else if(document.getElementById('email_type_option')) {
                document.getElementById('email_type_optionHidden').disabled = false;
            }
    
            $.ajax({
                url: $('#form').attr('action'),
                type: 'POST',
                data: $("#form").serialize(),
                beforeSend: function(xhr)
                {
                    var token = $('meta[name="csrf_token"]').attr('content');
                    if (token){
                        return xhr.setRequestHeader('X-CRSF-TOKEN', token);
                    }
                },
                dataType: 'JSON',
                success: function(data)
                {
                    if (data.status !== 'true') {
                        swal("Error!", "Details: "+data.message+" .", "error");
                    } else if (data.status == 'true') {
                        $('#modal').modal('hide');
                        swal("Success", "Your mailchimp list has been created. Go configure your form!", "success");
                        formSetting();
                        // console.log(data); // test purpose only
                    }
                },
                error: function(jqXHR, textStatus, errorThrown)
                {
                    swal("Error!", "Error creating mailchimp list. "+ errorThrown +".", "error");
                }
            });
        }
    

    这是我对控制器的输入请求:

    public function store(Request $request)
    {       
    
        $url = 'https://' . $this->dataCenter . '.api.mailchimp.com/3.0/lists/';
    
        $reqs = json_encode([
    
        'name' => $request->name,
        'permission_reminder' => $request->permission_reminder,
        'email_type_option' => $request->email_type_option = filter_var($request->email_type_option, FILTER_VALIDATE_BOOLEAN), // to make it boolean
        'contact' => $request->contact,
        // $request->contact[company],
        // $request->contact[address1],
        // $request->contact[city],
        // $request->contact[state],
        // $request->contact[zip],
        // $request->contact[country],
        'campaign_defaults' => $request->campaign_defaults
        // $request->campaign_defaults[from_name],
        // $request->campaign_defaults[from_email],
        // $request->campaign_defaults[subject],
        // $request->campaign_defaults[language]
        ]);
    
    
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_USERPWD, 'user:' . $this->apiKey);
        curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_TIMEOUT, 10);
        // curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); // if we use custom method (e.g: PUT/PATCH or DELETE)
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $reqs);
        $response = curl_exec($ch);
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);
    
        $result = json_decode($response, true);
    
        // Validation if get error
        if ( !empty($result['status']) ) {
            if ($result['status'] == 400) {
                return response()->json([
                    'status' => 'false',
                    'message' => $result['errors'][0]['message']
                ]);
            }
        } else {
            // Creating forms table
            $form = DB::table('forms')->insertGetId(
                [
                    'user_id' => auth()->user()->id,
                    'nama' => 'Untitled',
                    'description' => 'Describe your form here',
                    'button_name' => 'Submit',
                    'listID_mailchimp' => $result['id'], // get ID from response cURL
                    'created_at' => date('Y-m-d H:i:s'),
                    'updated_at' => date('Y-m-d H:i:s')
                ]
            );
            return response()->json([
                'status' => 'true'
            ]);
        }
    
    }
    

    看到了吗? 'email_type_option' 被过滤为布尔值。它对我有用。谢谢。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-22
      • 1970-01-01
      • 1970-01-01
      • 2011-06-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多