2016-01-27 80 views
0

我有以下jquery函數,它應該將數據發佈到一個php函數,該函數在該函數中將其傳遞給外部腳本進行處理。 下面是我的jQuery函數:Jquery PHP將Json數據發佈到外部URL

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數據傳遞給外部腳本?

+2

您被指定JSON變量order_confirmation訪問它() – msvairam

+0

可以顯示什麼是你在JavaScript –

+0

嘗試的var_dump($ _ POST)使用JSON變量的值;並檢查發佈內容。另請嘗試var_dump($ this-> input-> post()); – shapeshifter

回答

0

爲獲得原始數據後(在你的情況下)使用file_get_contents("php://input")

0

相反的data: json,你應該使用data: "json:json",

只需使用data: json,將發佈的數據,但它不會被分配給一個鍵。要訪問數據,您需要將其分配給一個密鑰。通過使用data: "json:json",,你可以用$json = $this->input->post('json');