当我单击由 javascript 生成的 html 中的按钮时,我想执行一个 php 脚本,我正在尝试使用 jquery ajax 并且我这样做了,但没有任何反应......任何帮助请?php 脚本正在工作,所以不是那样,我猜猜我在这个ajax调用中遗漏了一些东西......
阿贾克斯调用
$(".formBtn").click(function(){
$.ajax({
url: "script to call",
type: "post",
// callback handler that will be called on success
success: function(response, textStatus, jqXHR){
// log a message to the console
console.log("Hooray, it worked!");
alert("Working!");
},
// callback handler that will be called on error
error: function(jqXHR, textStatus, errorThrown){
// log the error to the console
console.log(
"The following error occured: "+
textStatus, errorThrown
);
},
// callback handler that will be called on completion
// which means, either on success or error
complete: function(){
// enable the inputs
$inputs.removeAttr("disabled");
}
});
});
PHP 脚本
$api_key = 'apikey';
$project_id = 'projectid';
$phone_id = 'phoneid';
$to_number = 'number';
$content = 'content';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL,
"urlblblbll");
curl_setopt($curl, CURLOPT_USERPWD, "{$api_key}:");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query(array(
'content' => $content,
'phone_id' => $phone_id,
'to_number' => $to_number,
)));
$json = curl_exec($curl);
if ($err = curl_error($curl)) { echo "$err\n"; }
curl_close($curl);
$res = json_decode($json, true);
var_dump($res); // do something with $res
}