【问题标题】:I do not get a response after a post JavaScript CodeIgniter发布 JavaScript CodeIgniter 后我没有得到响应
【发布时间】:2015-06-19 09:52:48
【问题描述】:

我有一个包含课程名称的下拉列表(例如“php”、“c”、“c#”...)。 我还有一个 onchange 事件,以便我可以检查下拉列表中的选项是否有效(在我的数据库中找到) - 它可能看起来很愚蠢,因为我自己将这些值放在从我的数据库中下拉,但我这样做是为了防止 SQL 注入

问题是我没有收到 verifycourse.php 的回复...所以这是我做错了。

function.js

function complete() {
    $.post("verifycourse/domain", {
        domain: $('#domain').val().toLowerCase()
    }).done(function (returnValue) {
        alert('after post'); //it does not alert
        var result = jQuery.parseJSON(returnValue);
        if (result['returnValue'] == 'ok') {
            alert('good domain');
        }
        else if (result['returnValue'] == 'wrong') {
            alert('wrong domain');
        }
    });
}

verifycourse.php

function index($param = 1)
  {
    parent::__construct();
    $this->load->model('user','',TRUE);
    $this->load->library( 'mysmarty' ); 
    $smarty = new Mysmarty ;

    if (isset($param) && $param == 'domain') {
        $this->isValidCourse($this->input->post('domain'));
    }
  }

    function isValidCourse($courseName)
    {
        $query = $this->db->get('admins');

        $arr = $this->user->getDomains($courseName);
        if(in_array($courseName, $arr))
            return json_encode(array('returnValue' => 'ok'));
        return json_encode(array('returnValue' => 'wrong'));
    }

user.php

function getDomains()
    {
        $query = $this->db->get('admins');

        $arr = array();
        foreach ($query->result() as $row) {
            if ($row->subject && !(in_array($row->subject, $arr)))
                $arr[] = $row->subject;
        }

        return $arr;
    }

home_view.tpl

<div class="modal-body">
                <form class="contact" name="contact">
                    <font color="red"><div id="errors"></div></font>
                    <input type="text" class="input-block-level" id="courseName" placeholder="Course name" name="courseName"/>
                    <br/>
                    <select class="input-block-level" id="domain" placeholder="Domain name" name="domain" onchange="complete();">
                      {foreach from=$myArray item=foo}
                          <option value="foo">{$foo}</option>
                      {/foreach}
                    </select>       
                </form>
            </div>

【问题讨论】:

  • 错误或拼写错误? f(in_aray($courseName, $arr)) 应该是 in_array
  • 不敢相信...错误,但仍然没有提醒
  • 您没有收到任何警报吗?从来没有alert("after post")
  • 连那个都没有……
  • 这个$smarty = new Mysmarty;不需要是这个$smarty = new Mysmarty();吗?

标签: javascript php jquery json codeigniter


【解决方案1】:

将您的 $.post 更改为 $.ajax 以捕获其他错误,如下所示:

$.ajax({
    type: "POST",
    url: "verifycourse/domain",
    data: {domain: $('#domain').val().toLowerCase()},
    success: function(msg){
        alert(  msg );
    },
    error: function(XMLHttpRequest, textStatus, error) {
        alert("some error");
        console.log(XMLHttpRequest);
        console.log(textStatus);
        console.log(error);
   }
});

【讨论】:

  • 我做了,但它提示“一些错误”...这如何帮助我找出我做错了什么?
  • 检查您的控制台。你知道怎么做吗?
  • 是的,它充满了 404 错误,我什至在 console.log(XMLHttpRequest);控制台.log(文本状态);控制台日志(错误); ...未捕获的ReferenceError:未定义错误
  • 这意味着它根本没有到达控制器。我认为问题在于您正在使用此 URL:"verifycourse/domain",但您需要使用 "verifycourse/index" 并且在您的 index() 方法中,您需要使用 $this-&gt;input-&gt;post() 捕获发布的变量
  • 或者你也可以把网址改成verifycourse/index/domain
【解决方案2】:
function complete() {
        $.post("verifycourse/domain", {
            domain: $('#domain').val().toLowerCase()
        },function (returnValue) {
            alert('after post'); //it does not alert
            var result = jQuery.parseJSON(returnValue);
            if (result['returnValue'] == 'ok') {
                alert('good domain');
            }
            else if (result['returnValue'] == 'wrong') {
                alert('wrong domain');
            }
        });
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-12-14
    • 1970-01-01
    • 1970-01-01
    • 2019-07-28
    • 2016-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多