当我通过 Mailchimp API 创建列表时,架构描述了布尔值,而不是字符串
Schema describes boolean, string found instead when I create list through Mailchimp API
我是开发 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"}]}"
我试图在我的复选框输入中输入布尔值,但它总是 return 那个错误。
如果我 console.log 在浏览器上显示它对布尔值的权利,但我不知道为什么在发送时仍然出现该错误。
我想将这个值 return 变成布尔值,而不是像错误所说的字符串。
如何解决?
终于解决了这个问题。我使用 ajax 将它发送到服务器或控制器(我使用 Laravel PHP 框架)实际上我发送的内容被转换为字符串(包括数字或布尔值)因为我使用 json_encode 当我想通过 Mailchimp 发送它时 API。
所以我在 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' 被过滤为布尔值。它对我有用。谢谢。
我是开发 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"}]}"
我试图在我的复选框输入中输入布尔值,但它总是 return 那个错误。
如果我 console.log 在浏览器上显示它对布尔值的权利,但我不知道为什么在发送时仍然出现该错误。
我想将这个值 return 变成布尔值,而不是像错误所说的字符串。 如何解决?
终于解决了这个问题。我使用 ajax 将它发送到服务器或控制器(我使用 Laravel PHP 框架)实际上我发送的内容被转换为字符串(包括数字或布尔值)因为我使用 json_encode 当我想通过 Mailchimp 发送它时 API。
所以我在 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' 被过滤为布尔值。它对我有用。谢谢。