2017-01-17 19 views
1

JavaScriptが動作していて、私がhistories/historyにアクセスしました。 alert'succes'と表示されます。あなたができるなら私を助けてください。CakePHP 3のAJAXリクエストからデータを取得できません

function sendFPost(){ 
    var hello = "Hello World"; 
    $.ajax({ 
     type: 'POST', 
     url: '/untitled/histories/history', 
     data: hello, 
     dataType: 'html', 
     success: function() { 
      alert('success'); 
     }, 
     error: function() { 
      alert('error'); 
     } 
    }); 
} 

sendFPost(); 
public function history() 
{ 
    print_r($this->request->data); 
} 
+1

チェックこれをhttp://stackoverflow.com/questions/20327049/how-to-retrieve-data-sent-by-ajax- in-cakephp – M14

+0

try:data: 'hello'を開始してから、M14のリンクのようにJSONを送信してみてください。 – cornelb

答えて

0

私は、JSONリクエストを使用することをお勧めしたいと思います:

Ajaxリクエスト:コントローラで

function sendFPost(){ 
var name = 'abc'; 
var email = '[email protected]'; 
var phone = 1234; 
$.ajax({ 
    type: 'POST', 
    url: '/untitled/histories/history.json', // json request 
    data: "name="+name+"&email="+email+"&phone="+phone, 
    dataType: 'html', 
    success: function (response) { 
     console.log(response); 
    }, 
    error: function() { 
     alert('error'); 
    } 
}); 
} 
sendFPost(); 

public function history() 
{ 
    if($this->request->is('post')) { 
    $data = $this->request->data; 
    $this->set([ 
     'data'=>$data, 
     '_serialize'=>'data' // this will appear within success of ajax 
    ]); 
    } 
} 

以前に行っていない場合は、ルートでjson拡張を有効にする必要があります。詳細について

https://book.cakephp.org/3.0/en/development/rest.html

https://book.cakephp.org/3.0/en/views/json-and-xml-views.html#jsonp-responses

+0

あなたの答えはあまりにも辛いです)私の問題はほとんど解決しますが、JS変数の値をPHPに送信してPHPで操作しなければなりません。もう一度私を助けてくれますか? –

+0

私はすでにそれを解決しています。もう一度お返事ありがとうございます) –

+0

私はあなたがすでにそれをしていると思います。しかし、私は答えを更新しました。 –

関連する問題