【问题标题】:$Ajax request in codeigniter also check if request is correctcodeigniter 中的 $Ajax 请求还检查请求是否正确
【发布时间】:2017-05-31 11:21:07
【问题描述】:

我已经尝试了很多并搜索了很多,我做了和我看到的一样的事情,但我不知道我哪里出错了我是 codeigniter 的新手。你能告诉我代码哪里出错了吗?我想根据选择的条形码在我的输入字段中设置产品名称。我正在尝试获取产品名称并尝试使用 AJAX 设置它,但它不起作用。请您告诉我代码中的错误在哪里?

这是我在 Main.php 中的控制器函数

 public function get_product()
    {   

        if (!$this->input->is_ajax_request()) {
           exit('No direct script access allowed');
        }
        $barcode = $_POST['barcode'];
        echo $barcode;
        exit;
        $data['result'] = $this->Item_model->get_product_using_barcode($barcode);
        print_r($data);
        exit;



    }

我在 js 文件中的 Ajax 代码:

$("#brcode").each(function(){

           $("#brcode").change(function(){

                    let value = $("#brcode").val();

                    $.ajax({
                            url: '<?php echo site_url("main/get_product"); ?>',
                            type: 'POST',
                            data: {'barcode': $('#brcode option:selected').val() },
                            dataType: 'json',
                            success: function(data) {
                            //console.log(data);
                            alert(data);
                        }
                    })
           });

        });

模型功能:-

public function get_product_using_barcode($barcode){
         $query = $this->db->get_where('items',array('barcode' => $barcode));
         $result = $query->row();
         return $result;


     }

请告诉我我的代码哪里出错了?

【问题讨论】:

  • 您正在请求 json 但没有返回 json
  • 尝试json_encode($data);访问成功回调函数中的响应
  • 我尝试从请求中删除 datatype:json 但仍然无法正常工作!如果你能看出我的 js 代码是否正确?我是否向正确的函数发送请求?
  • 检查您的浏览器网络标签? ajax 请求会进行吗?
  • 不,它没有显示任何 ajax 请求。我在我的 js 代码中发送 ajax 请求是否有任何错误?

标签: php jquery ajax codeigniter


【解决方案1】:

试试这个代码它对我有用。希望这会对你有所帮助。

JQuery:

$(document).ready(function() {
     $("#brcode").change(function(){
        let value = $("#brcode").val();
        $.ajax({
                // URL should be include index.php
                url: '<?php echo site_url("index.php/main/get_product"); ?>',
                type: 'POST',
                data: {'barcode': $('#brcode option:selected').val() },
                dataType: 'json',
                success: function(data) {
                //console.log(data);
                alert(data);
            }
        })
    });
 });

控制器:

public function get_product()
{   
    if (!$this->input->is_ajax_request()) {
       exit('No direct script access allowed');
    }
    $barcode = $_POST['barcode'];
    $data['result'] = $this->Item_model->get_product_using_barcode($barcode);
    return json_encode $data;
}

型号:

public function get_product_using_barcode($barcode){
     $query = $this->db->get_where('items',array('barcode' => $barcode));
     $result = $query->row();
     return $result;
 }

您好!

【讨论】:

  • &lt;?php echo site_url("index.php/main/get_product"); ?&gt; 这里不需要index.php...因为siteurl会自动生成这个。
【解决方案2】:

你能像这样改变js代码吗?并尝试在控制台上查看。

  $(document).ready(function() {
         $("#brcode").change(function(){
                var value = $("#brcode").val();
                $.ajax({
                        url: '<?php echo site_url("main/get_product"); ?>',
                        type: 'POST',
                        data: {'barcode': $('#brcode option:selected').val() },

                        success: function(data) {
                        //console.log(data);
                        alert(data);
                    }
                })
       });
 });

【讨论】:

  • jquery-2.1.1.min.js:4 POST localhost/retail/%3C?php%20echo%20site_url(%22main/… 403 (Forbidden) 这个错误我认为发送 ajax 请求有问题。
  • 您好,我解决了这个问题,ajax 请求现在正在工作,但是有人可以告诉我如何从我的响应数据中获得的数组对象中获取特定值???
【解决方案3】:

您在使用dataType: 'json' 时在本节中出错了,您的ajax 应该返回一个json 对象。

PHP 标记在 js 文件中不起作用,因此您应该将 ajax 请求放在页面的页脚或硬编码您的 url。例如:url:"http://localhost/retail/main/get_product"

我也认为let value = $("#brcode").val(); 应该是var value = $("#brcode").val();

所以你的控制器代码应该是

public function get_product()
{  
        $data = array();
        if (!$this->input->is_ajax_request()) {
           exit('No direct script access allowed');
        }
        $barcode = $_POST['barcode'];

        $data['result'] = $this->Item_model->get_product_using_barcode($barcode);
        echo json_encode(array("posted_data"=>$_POST,"database_data"=>$data['result']));
        exit;
}

在成功的 ajax 中使用这个

 success: function(data) {
      console.log("Posted data");
      console.log(data.posted_data);
      console.log("Database data");
      console.log(data.database_data);
  }

【讨论】:

  • XMLHttpRequest 无法加载 url:"localhost/retail/main/get_product"。跨源请求仅支持协议方案:http、data、chrome、chrome-extension、https。它没有加载网址
  • @PranaySute 你为什么要做跨域你不是从同一个文件夹生成ajax请求吗?
  • 我只想根据选择的条形码设置产品名称,所以我试图从数据库中获取产品名称,但 url 在控制台中显示错误我不知道我在做什么发送请求时做错了。
  • 是的,我解决了我自己尝试过的问题,代码完全按照我想要的方式工作,但感谢@Praveen :) ..!!
猜你喜欢
  • 1970-01-01
  • 2012-07-26
  • 2015-05-27
  • 2012-01-20
  • 2021-06-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多