2017-01-05 44 views
0

我有我的應用程序做出反應localhost上運行:8000收到JSON數據 - AJAX

我試圖發送一個AJAX POST請求,並通過數據:在本地主機上運行的3000 我笨API JSON格式,但不知道如何接受它在我的控制器......這裏是我有什麼

REACT

import React, { Component } from 'react'; 
import Request from 'superagent'; 

class App extends Component { 
    constructor(){ 
    super() 
     this.state = { 
     email: '', 
     password: ''  
    } 
    } 
    updateEmail(e){ 
    this.setState({ 
     email: e.target.value 
    }) 
    } 
    updatePassword(e){ 
    this.setState({ 
     password: e.target.value 
    }) 
    } 
    createUser(e){ 
    var query = 'star'; 
    e.preventDefault() 
    console.log(this.state.email) 
    var url = `http://localhost:8888/CI-React-API/React_api/create`; 
    Request.post(url) 
    .set('Content-Type', 'application/json') 
    .send({ email: 'test' }) 
    .then((response) => { 
     console.log(response) 
    }); 
    } 
    render() { 
    return (
     <div className="App"> 
     <div className="App-header"> 
      <form onSubmit={this.createUser.bind(this)}> 
      <input type="text" 
      name="email" 
      onChange={this.updateEmail.bind(this)} 
      value={this.state.email} 
      placeholder="email"/> 
      <br/> 
      <input type="text" 
      name="password" 
      value={this.state.password} 
      onChange={this.updatePassword.bind(this)} 
      placeholder="password"/> 
      <br/> 
      <input type="submit" value="submit"/> 
      </form> 
     </div> 
     </div> 
    ); 
    } 
} 

export default App; 

<?php 
defined('BASEPATH') OR exit('No direct script access allowed'); 

class React_api extends CI_Controller { 

    public function create() 
    { 

$obj=json_decode(file_get_contents('php://input'), true); 
     // $email = $this->input->post('email'); 

     // echo $email; 
     var_dump(file_get_contents('php://input')); 
     die; 
     // IDEA $this->load->view('welcome_message'); 
    } 
} 
+0

檢查答案,如果有用的話接受它,並投了謝謝 –

回答

0

變更網址

var url = `http://localhost:8888/CI-React-API/React_api/create`; 

var url = 'http://localhost:8888/react_api/create'; 

OR 

var url = '<?php echo base_url("react_api/create");?>'; 

和負載url幫手控制器如下:

<?php 
defined('BASEPATH') OR exit('No direct script access allowed'); 

class React_api extends CI_Controller { 

    function __construct() 
    { 
     parent::__construct(); 
     $this->load->helper('url'); 
    } 

    public function create() 
    { 

     $email = $this->input->post('email'); 
     echo $email; 

$obj=json_decode(file_get_contents('php://input'), true); 

     var_dump(file_get_contents('php://input')); 
     die; 
     // IDEA $this->load->view('welcome_message'); 
    } 
} 
+0

Hikmat,我的反應計劃與我的codeigniter不同。他們是分開的。 –

+0

然後只嘗試'var url ='http:// localhost:8888/react_api/create'' –

+0

但您幫我找到了解決方案。我所需要做的就是在codeigniter框架中啓用URL –