douquanzhan0315 2015-02-26 03:14
浏览 150

通过AJAX以JSON格式提交表单

I'm trying to submit a HTML form to a server and it fails. I have several issues here.

  1. I'm making a CORS request. It is a request by the client and I can't do anything about it. ( My app resides in localhost:3000/login.html & My server resides in http://localhost:3001/Login/auth )

  2. The server in localhost:3001 accepts JSON requests and give back a JSON Responce.

(I have disabled the web-security in the browser to allow the CORS Requests)

So, I have created the HTML Form and tried to submit it with AJAX. Here you can see the code.

$(function(){
    $("#loginform").submit(function(e){

        e.preventDefault();

//      var data = $("#loginform").serialize();

            var data =  {"username" : $("#username").val(),"password" : $("#password").val()};

        alert(JSON.stringify(data));

        $.ajax({
            type: "POST",
            url: "http://localhost:3001/Login/auth",
            data: data,

            contentType: "application/json",
            crossDomain : true,
            dataType: "json",
            success: function(data) {

                alert("success" + JSON.stringify(data));

            },
            error:function(data){
                alert('error : '+JSON.stringify(data));
            }

        });

    });
});

I have created the mock server in PHP (codeigniter). Here is the code for that.

public function auth(){

        $input_data = json_decode(trim(file_get_contents('php://input')), true);

          $debug_export = var_export(file_get_contents('php://input'), true);
          file_put_contents('new.txt',$debug_export . PHP_EOL, FILE_APPEND);

        $user = 'admin';
        $pass = 'admin';
        $token = '1';

        foreach($input_data as $key=>$value) {
            if ($key === 'username') {
                $this_user = $value;
            }
            if ($key === 'password') {
                $this_password = $value;
            }

        }

        if(isset($this_user) && isset($this_password)){
            if($this_password === $pass && $this_user === $user){
                echo json_encode(array('token'=>$token));
            }else{
                echo json_encode(array('token'=>'null'));

            }
        }


        return 0;

    }

When I submit a form, I get a response like this.

....
<p>Severity: Warning</p>
<p>Message:  Invalid argument supplied for foreach()</p>
<p>Filename: controllers/Login.php</p>
<p>Line Number: 77</p>
....

My 'new.txt' file has the log details as following.

''
'username=admin&password=admin'

I have really no idea what I'm doing wrong here. Can anyone help me out here?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 关于多单片机模块化的一些问题
    • ¥30 seata使用出现报错,其他服务找不到seata
    • ¥35 引用csv数据文件(4列1800行),通过高斯-赛德尔法拟合曲线,在选取(每五十点取1点)数据,求该数据点的曲率中心。
    • ¥20 程序只发送0X01,串口助手显示不正确,配置看了没有问题115200-8-1-no,如何解决?
    • ¥15 Google speech command 数据集获取
    • ¥15 vue3+element-plus页面崩溃
    • ¥15 像这种代码要怎么跑起来?
    • ¥15 安卓C读取/dev/fastpipe屏幕像素数据
    • ¥15 pyqt5tools安装失败
    • ¥15 mmdetection