我有以下jquery函数,该函数应该将数据发布到php函数,在此过程中它将它传递给外部脚本进行处理。 下面是我的jquery函数:
function order_confirmation(json) {
$.ajax({
type: 'POST',
url: "<?php echo base_url(); ?>home/confirm_order_received/",
data: json,
contentType: 'application/json',
dataType: 'json',
success: function (data) {
console.log(data);
},
failure: function (errMsg) {
console.log(errMsg);
}
});
}
此函数应该将json数据传递给我的php函数,该函数位于此处:
function confirm_order_received() {
$json = $this->input->post('json');
// Initialize curl
$curl = curl_init();
$externalscriptaddress = "http://197.237.132.178/api/order/order_upsert";
// Configure curl options
$opts = array(
CURLOPT_URL => $externalscriptaddress,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => $json
);
// Set curl options
curl_setopt_array($curl, $opts);
// Get the results
$result = curl_exec($curl);
// Close resource
curl_close($curl);
echo $result;
}
我有一个问题,我应该如何从php端访问数据,我尝试通过帖子访问它:$json = $this->input->post('json');
并将其传递给CURLOPT_POSTFIELDS => $json
但我收到错误因为什么都没有通过。请告知我如何将json数据传递给外部脚本?
答案 0 :(得分:0)
要获取原始帖子数据(在您的情况下),请使用file_get_contents("php://input")
答案 1 :(得分:0)
而不是data: json,
,您应该使用data: "json:json",
仅使用data: json,
将发布数据,但不会将其分配给密钥。要访问数据,您需要将其分配给密钥。使用data: "json:json",
,您可以使用$json = $this->input->post('json');