通过AJAX发送http请求
问题描述:
我有一个SMS API,我需要用它在我正在构建的网站上发送文本消息。网站上有一个表格收集一些细节和电话号码,信息必须转到表格中指定的电话号码。通过AJAX发送http请求
的API链接我是:
我怎样才能做到使用Ajax这个POST请求?
var formData = {
'name': name,
'location': location,
'mobile': mobile
};
$.ajax({
type: 'POST',
url: 'upload.php',
data: formData,
dataType: 'json',
encode: true
})
这是我在网站上使用的表格,我可以使用这个API吗?
答
在你upload.php的文件中作出这样的卷曲要求:
upload.php的:
$encodedMsg = urlencode($message);
$url = // url api url with mobile number and url emcoded message in it
// parameter to make a curl request
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$APIresponse = curl_exec($ch);
curl_close($ch);
$res = json_decode($APIresponse, true);
if($res['ErrorCode'] == '000') // check for the response return by the API
{
$response = 'Success';
}
else
{
$response = 'Failed';
}
echo $response;
// Use this response in your ajax success function
取决于您的API,阅读文档 – madalinivascu
正如@madalinivascu它说依赖于你的API。如果你可以提供短信网关的名字,可能有人会指引你正确的方向。 –