jquery ajax不能与codeigniter rest api一起工作

时间:2017-06-16 19:26:34

标签: php jquery ajax codeigniter

当我用chrome中的POSTMAN测试GET,POST,PUT,DELETE时,我的休息api完全正常工作 但当我在index.html中使用jquery ajax访问它时,这不起作用卡在“正在连接......”

这是我在codeigniter中的控制器rest api

    <?php

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

require APPPATH . '/libraries/REST_Controller.php';
use Restserver\Libraries\REST_Controller;

class Belajar extends REST_Controller {

    function __construct($config = 'rest') {
    header('Access-Control-Allow-Origin: *');
    header("Access-Control-Allow-Headers: X-API-KEY, Origin, X-Requested-With, Content-Type, Accept, Access-Control-Request-Method");
    header("Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE");
    $method = $_SERVER['REQUEST_METHOD'];
    if($method == "OPTIONS") {
        die();
    }
        parent::__construct($config);
        $this->load->database();
    }

    //Menampilkan data kontak
    function index_get() {
        $id = $this->get('id');
        if ($id == '') {
            $kontak = $this->db->get('telepon')->result();
        } else {
            $this->db->where('id', $id);
            $kontak = $this->db->get('telepon')->result();
        }
        $this->response($kontak, 200);
    }


    //Masukan function selanjutnya disini
    function index_post() {
        $data = array(
                    'id'           => $this->post('id'),
                    'nama'          => $this->post('nama'),
                    'nomor'    => $this->post('nomor'));
        $insert = $this->db->insert('telepon', $data);
        if ($insert) {
            $this->response($data, 200);
        } else {
            $this->response(array('status' => 'fail', 502));
        }
    }

    function index_put() {
        $id = $this->put('id');
        $data = array(
                    'id'       => $this->put('id'),
                    'nama'          => $this->put('nama'),
                    'nomor'    => $this->put('nomor'));
        $this->db->where('id', $id);
        $update = $this->db->update('telepon', $data);
        if ($update) {
            $this->response($data, 200);
        } else {
            $this->response(array('status' => 'fail', 502));
        }
    }

    function index_delete() {
        $id = $this->delete('id');
        $this->db->where('id', $id);
        $delete = $this->db->delete('telepon');
        if ($delete) {
            $this->response(array('status' => 'success'), 201);
        } else {
            $this->response(array('status' => 'fail', 502));
        }
    }

}

?>

这是我的index.html

    <!DOCTYPE html>
<html>
    <head>
        <meta name="format-detection" content="telephone=no">
        <meta name="msapplication-tap-highlight" content="no">
        <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width">
        <link rel="stylesheet" type="text/css" href="css/index.css">
        <script src="js/jquery.min.js"></script>
        <title>Hello World</title>
    </head>
    <body>
        <div id="container">

  <span class="input">
    <input type="text" class="input__field" id="name" />
    <label for="input-1" class="input__label">
      <span class="input__label-content">Nama</span>
  </label>
  </span>

  <span class="input">
    <input type="text" class="input__field" id="nomor" />
    <label for="input-2" class="input__label">
      <span class="input__label-content">Nomor</span>
    </label>
  </span>
  <input type=button id="send-button" type="button" value='Send' />

</div>

    </body>
</html>
 <script>
  $(document).ready(function()
 {
 $("#send-button").click(function(){


 var name=$("#name").val();
 var nomor=$("#nomor").val();
 var dataString="id=&amp;name="+name+"&amp;nomor="+nomor;
 $.ajax({
 type: "POST",
 url:"http://localhost:8080/apici/belajar",
 data: dataString,
 crossDomain: false,
 cache: false,
 beforeSend: function(){ $("#send-button").val('Connecting...');},
 success: function(data){
 if(data=="success")
 {
 alert("inserted");
 $("#send-button").val('submit');
 }
 else if(data=="error")
 {
 alert("error");
 }
 }
 });

 });
 });
 </script>

我检查了网址,它在浏览器中工作。 thnx求助:)

0 个答案:

没有答案