会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 133928个问题

老师,用post请求接收不到前台返回的数据

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="js/jquery-3.5.1.js"></script>
</head>
<body>
<span>用户名:</span><input type="text" class="username"><br>
<span>密码:</span><input type="text" class="password"><br>
<button>get无参请求</button>
<button>get有参请求</button>
<button>post请求</button>
<script>
    /*
    Ajax
    描述 Ajax全称为Asynchronous JavaScript and XML
         是一种前后台数据交互的手段
    说明 原生的Ajax实现比较麻烦,需要借助xmlhttprequest对象来构建
         而今天我们实现的是jQuery已经封装好的现成方法
    说明 get无参请求 一般用来向后台获取数据使用,不会发送给后台内容
         get有参请求 一般向贴吧、社区等需要根据页码或者指定的属性来获取数据使用,
                     这种情况下参数一般都比较小
         post请求    一般用于登录、注册等保密性高的地方
     */
    var $usernameInput=$('.username');
    var $passwordInput=$('.password');
    var $btns=$('button');
    $btns.eq(0).click(function () {
        $.ajax({
            //请求方式为get
            type:'get',
            //指明请求要发送到的php后台地址
            url:'10jqAjax请求.php',
            //交互的数据格式为json
            dataType:'json',
            //当后台返回数据时,本函数自动执行,用来获取后台返回的具体数据内容
            success:function (res) {
                console.log(res);
                console.log(res.info);
            }
        });
    })
    $btns.eq(1).click(function () {
        $.ajax({
            //请求方式为get
            type:'get',
            //指明请求要发送到的php后台地址
            url:'10jqAjax请求.php?username='+$usernameInput.val()+"password"+$passwordInput.val(),
            //交互的数据格式为json
            dataType:'json',
            //当后台返回数据时,本函数自动执行,用来获取后台返回的具体数据内容
            success:function (res) {
                console.log(res);
                console.log(res.info);
            }
        });
    })
    $btns.eq(2).click(function () {
        $.ajax({
            //请求方式为post
            type:'post',
            //指明请求要发送到的php后台地址
            url:'10jqAjax请求.php',
            date:{
                uname:$usernameInput.val(),
                upass:$passwordInput.val()
            },
            //交互的数据格式为json
            dataType:'json',
            //当后台返回数据时,本函数自动执行,用来获取后台返回的具体数据内容
            success:function (res) {
                console.log(res);
                console.log(res.info);
            }
        });
    })
</script>
</body>
</html>

<?php
     //post请求
     //php中构建数组结构
     $successArr= array('msg'=>'ok','info'=>$_POST);
     //通过echo和json_encode(对象)方法将这个数组转换成json,并返回到前端
     echo json_encode($successArr);
?>

blob.png

WEB前端全系列/第五阶段:前后端交互/PHP、数据库编程与设计 23401楼
JAVA 全系列/第六阶段:项目管理与SSM框架/SpringMVC 23405楼
Python 全系列/第二阶段:Python 深入与提高/游戏开发-坦克大战 23409楼
JAVA 全系列/第十八阶段:亿级高并发电商项目_架构/编码(旧)/电商:基于FastDFS+Nginx+Kinkeditor实现商品新增 23410楼

package Test;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * 接收客户端消息的线程类
 */
class ChatReceive extends Thread{
    private Socket socket;

    public ChatReceive(Socket socket) {
        this.socket = socket;
    }


    @Override
    public void run() {
        this.receiveMsg();
    }
    /**
     * 实现接收客户端发送的消息
     */
    private void receiveMsg(){
        BufferedReader br=null;
        try{
            br=new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
            while (true){
                String msg=br.readLine();
                synchronized ("abc"){
                    //把读取到的数据写入公共数据区
                    ChatRoomServer.buf="["+this.socket.getInetAddress()+"]"+msg;
                    "abc".notifyAll();

                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (br!=null){
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
         
            if (socket!=null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
    }
}

/**
 * 向客户端发送消息的线程类
 */
class ChatSend extends Thread{
    private Socket socket;

    public ChatSend(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        this.SendMsg();
    }
    /**
     * 将公共数据区的数据发送到客户端
     */
    private void SendMsg(){
        PrintWriter pw=null;
        try{
            pw=new PrintWriter(this.socket.getOutputStream());
            while (true){
                synchronized("abc"){
                    //让线程处于等待状态
                    "abc".wait();
                    pw.println(ChatRoomServer.buf);
                    pw.flush();

                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (pw!=null){
                pw.close();
            }
            if (this.socket!=null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
public class ChatRoomServer {
    public static  String buf;
    public static void main(String[] args) {
        System.out.println(" 1.0");
        System.out.println("Listen at 8888");
        ServerSocket serverSocket=null;
        try {
            serverSocket=new ServerSocket(8888);
            while (true){
                Socket socket=serverSocket.accept();
                System.out.println("连接到 "+socket.getInetAddress());
                new ChatReceive(socket).start();
                new ChatSend(socket).start();
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (serverSocket!=null){
                try {
                    serverSocket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

老师我这里代码怎么报错,没找到问题

image.png

JAVA 全系列/第二阶段:JAVA 基础深化和提高/网络编程(旧) 23413楼
Python 全系列/第三阶段:Python 网络与并发编程/网络通信 23414楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637