Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php jquery ajax不能与codeigniter rest api一起使用_Php_Jquery_Ajax_Codeigniter - Fatal编程技术网

Php jquery ajax不能与codeigniter rest api一起使用

Php jquery ajax不能与codeigniter rest api一起使用,php,jquery,ajax,codeigniter,Php,Jquery,Ajax,Codeigniter,当我在chrome中用POSTMAN测试GET、POST、PUT、DELETE时,我的RESTAPI已经完全工作了, 但是当我在index.html中使用jquery ajax访问它时,它就不起作用了。卡在“连接…”中。 这是我在codeigniter中的控制器RESTAPI <?php defined('BASEPATH') OR exit('No direct script access allowed'); require APPPATH . '/libraries/RE

当我在chrome中用POSTMAN测试GET、POST、PUT、DELETE时,我的RESTAPI已经完全工作了, 但是当我在index.html中使用jquery ajax访问它时,它就不起作用了。卡在“连接…”中。

这是我在codeigniter中的控制器RESTAPI

    <?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>

你好,世界
纳米
诺莫
$(文档).ready(函数()
{
$(“#发送按钮”)。单击(函数(){
var name=$(“#name”).val();
var nomor=$(“#nomor”).val();
var dataString=“id=&;name=“+name+”&;nomor=“+nomor;
$.ajax({
类型:“POST”,
url:“http://localhost:8080/apici/belajar",
数据:dataString,
跨域:false,
cache:false,
beforeSend:function(){$(“#发送按钮”).val('Connecting…');},
成功:功能(数据){
如果(数据=“成功”)
{
警告(“插入”);
$(“#发送按钮”).val('submit');
}
else if(数据==“错误”)
{
警报(“错误”);
}
}
});
});
});
我检查了url,它在浏览器中工作。
thnx感谢您的帮助:)

对于需要的文件,您不需要
使用Restserver\Libraries\REST\U控制器行。对于需要的文件,您不需要
使用Restserver\Libraries\REST\u控制器行。