duanduan8439 2017-03-29 10:01
浏览 53

在codeigniter中使用Ajax post调用导致内部服务器错误

This is the error message found on google chrome:
POST http://localhost/display/register/registerUser 500 (Internal Server Error)

My project file name is "display".

One controller file named "register" ,it has a function called "registerUser". All it does is getting user's input, and compare it with database to see if the name is used or not, if valid store it into database and give them a success message, if not valid give a error message.

$username=$this->input->post('name');
    $password=$this->input->post('password');

    $result=$this->user->checkExistUser($username);

    if(!$result){

        $data=array(
            'username'=>$username,
            'password'=>md5($password)
            );


    $this->user->register($data);

    echo "<script>alert('You have success registered');</script>";
    $this->load->view('home_view');
    }
    else{

        echo "<script>alert('Sorry, this email address has been used!');</script>";
        $this->load->view('registerView');
    }

This is my view file named registerView

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?><!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Welcome to Stock game</title>
<script 
src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js">
</script>

</head>
<body>

<div id="container">
<h1>Welcome to Stock game!</h1>

<div id="body">
    <p>This is the register page</p>

</div>

<div>

    <form  name="userform" id="userform" >
    <input type="text" name="name" placeholder="Name"/>
    <input type="text" name="password" placeholder="Password"/>

    <button name="submit" value="submit">Register</button>
    </form>
</div>
    <script type="text/javascript">
    $(document).ready(function(){
        $('form#userform').on('submit',function(e){
            e.preventDefault();

    var name=$("#name").val();
    var password=$("#password").val();
    $.ajax({

        type:'POST',
        url:'register/registerUser',
        data:
        {'name':JSON.stringify(name),'password':JSON.stringify(password)},
        dataType:'JSON',
        success: function(response){
            if(response=="success"){
                alert("Yes login");
            }
        },
       error: function (xhr, ajaxOptions, thrownError) {
       alert(xhr.status);
       alert(xhr.responseText);
       alert(thrownError);
   }
   });
        });
    });
    </script>
    </div>
    <br/>
    </body>
    </html>

I have tested the controller and model class with a simple form post(form action=xxx, method=post) and it is no error. I searched almost every stackoverflow questions about this, but i still cannot fix it, I hope someone can help me.

  • 写回答

3条回答 默认 最新

  • doukongyong44772 2017-03-29 10:05
    关注

    Append the base url in your ajax

    url:<?php echo base_url(); ?>'register/registerUser',
    
    评论

报告相同问题?

悬赏问题

  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装