2016-12-14 4 views
0

Mailchimpアプリケーションの開発時に新規です。 私はMailchimp APIを通じてリストを作成しようとしています。 誰もこのエラーで同じ問題がありますか?ここでMailchimp APIを使用してリストを作成すると、スキーマにboolean、stringが見つかりました

は、チェックボックスの入力のための私の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これはブール値に正しく表示されますが、私はそれを送信するときになぜまだそのエラーがあるのか​​わかりません。

私はこの値をブール値に戻したいのですが、エラーのように文字列ではありません。 修正方法?

答えて

0

最後に、私はこの問題を解決しなければなりません。私はサーバーやコントローラ(私はLaravel PHPフレームワークを使用)に送信するためにajaxを使用し、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'はブール値にフィルターされます。それは私のために働く。ありがとうございました。

関連する問題