Twilio Ajax 响应总是触发 'Fail',尽管调用成功
Twilio Ajax response always triggers 'Fail', Despite successfully making call
我正在尝试制作一个简单的 Twilio 应用程序,我主要遵循本教程:https://www.twilio.com/docs/voice/tutorials/click-to-call-php
然而,我对它进行了微调和简化以满足我的需要,尽管问题元素似乎没有任何不同。
Ajax :
$('#twilio_click_form').on('submit', function(e) {
// Prevent submit event from bubbling and automatically submitting the form
e.preventDefault();
// Call our ajax endpoint on the server to initialize the phone call
$.ajax({
url: '[...]/twilio_click_call',
method: 'POST',
dataType: 'json',
data: {
userPhone: $('#userPhone').val()
}
}).done(function(data) {
// The JSON sent back from the server will contain a success message
alert(data.message);
}).fail(function(error) {
alert('error');
alert(JSON.stringify(error));
});
});
PHP:
public function twilio_click_call()
{
$twilio_creds = array(
'TWILIO_ACCOUNT_SID' => 'xxxx',
'TWILIO_AUTH_TOKEN' => 'xxxx',
'TWILIO_NUMBER' => 'xxxx'
);
$userPhone = $this->input->post('userPhone');
// Create authenticated REST client using account credentials
$client = new Twilio\Rest\Client(
$twilio_creds['TWILIO_ACCOUNT_SID'],
$twilio_creds['TWILIO_AUTH_TOKEN']
);
try
{
$client->calls->create($userPhone, $twilio_creds['TWILIO_NUMBER'],
array("url" => 'http://demo.twilio.com/docs/voice.xml')
);
}
catch (Exception $e)
{
// Failed calls will throw
return $e;
}
return array('message' => 'Call incoming!');
}
调用启动并完美运行,但是 Ajax 响应总是触发 .fail(),而不是 .done() 方法 - 无法确定原因。
您的代码中有两处错误。
1. 在 ajax 调用中,您已将 datType 定义为 json,因此它期望 contentType 也为 json,但您返回的是第一个问题的数组
2. 您在 url
中调用 url
使用此代码:
public function twilio_click_call()
{
$twilio_creds = array(
'TWILIO_ACCOUNT_SID' => 'xxxx',
'TWILIO_AUTH_TOKEN' => 'xxxx',
'TWILIO_NUMBER' => 'xxxx'
);
$userPhone = $this->input->post('userPhone');
// Create authenticated REST client using account credentials
$client = new Twilio\Rest\Client(
$twilio_creds['TWILIO_ACCOUNT_SID'],
$twilio_creds['TWILIO_AUTH_TOKEN']
);
try
{
$client->calls->create($userPhone, $twilio_creds['TWILIO_NUMBER'],
array("url" => 'http://demo.twilio.com/docs/voice.xml')
);
}
catch (Exception $e)
{
// Failed calls will throw
echo json_encode(array($e));
}
echo json_encode(array('message' => 'Call incoming!'));
}
我正在尝试制作一个简单的 Twilio 应用程序,我主要遵循本教程:https://www.twilio.com/docs/voice/tutorials/click-to-call-php 然而,我对它进行了微调和简化以满足我的需要,尽管问题元素似乎没有任何不同。
Ajax :
$('#twilio_click_form').on('submit', function(e) {
// Prevent submit event from bubbling and automatically submitting the form
e.preventDefault();
// Call our ajax endpoint on the server to initialize the phone call
$.ajax({
url: '[...]/twilio_click_call',
method: 'POST',
dataType: 'json',
data: {
userPhone: $('#userPhone').val()
}
}).done(function(data) {
// The JSON sent back from the server will contain a success message
alert(data.message);
}).fail(function(error) {
alert('error');
alert(JSON.stringify(error));
});
});
PHP:
public function twilio_click_call()
{
$twilio_creds = array(
'TWILIO_ACCOUNT_SID' => 'xxxx',
'TWILIO_AUTH_TOKEN' => 'xxxx',
'TWILIO_NUMBER' => 'xxxx'
);
$userPhone = $this->input->post('userPhone');
// Create authenticated REST client using account credentials
$client = new Twilio\Rest\Client(
$twilio_creds['TWILIO_ACCOUNT_SID'],
$twilio_creds['TWILIO_AUTH_TOKEN']
);
try
{
$client->calls->create($userPhone, $twilio_creds['TWILIO_NUMBER'],
array("url" => 'http://demo.twilio.com/docs/voice.xml')
);
}
catch (Exception $e)
{
// Failed calls will throw
return $e;
}
return array('message' => 'Call incoming!');
}
调用启动并完美运行,但是 Ajax 响应总是触发 .fail(),而不是 .done() 方法 - 无法确定原因。
您的代码中有两处错误。 1. 在 ajax 调用中,您已将 datType 定义为 json,因此它期望 contentType 也为 json,但您返回的是第一个问题的数组 2. 您在 url
中调用 url使用此代码:
public function twilio_click_call()
{
$twilio_creds = array(
'TWILIO_ACCOUNT_SID' => 'xxxx',
'TWILIO_AUTH_TOKEN' => 'xxxx',
'TWILIO_NUMBER' => 'xxxx'
);
$userPhone = $this->input->post('userPhone');
// Create authenticated REST client using account credentials
$client = new Twilio\Rest\Client(
$twilio_creds['TWILIO_ACCOUNT_SID'],
$twilio_creds['TWILIO_AUTH_TOKEN']
);
try
{
$client->calls->create($userPhone, $twilio_creds['TWILIO_NUMBER'],
array("url" => 'http://demo.twilio.com/docs/voice.xml')
);
}
catch (Exception $e)
{
// Failed calls will throw
echo json_encode(array($e));
}
echo json_encode(array('message' => 'Call incoming!'));
}