ajax 异步请求并获取返回数据

2020-10-13 更新
以下是核心代码:注意要引用jquery文件
   //get请求方式
   $(function () {
        $('#btnLogin').click(function () {
            $.ajax({
                url: "http://localhost:8080/API/Login?account=usr1&passwd=123456",
                type: "get", //请求方式为get
                dataType: "json", //返回数据格式为json
                success: function (data) { //请求成功完成后要执行的方法
				//假如 服务端返回 {"errCode":0,"errMessage":"帐号或密码错误","companyUUID":null}
				//则下面弹出 帐号或密码错误
					alert(data.errMessage);
                }
            });
        });
    })
    //post请求方式
    $(function () {
        $('#btnLogin').click(function () {
             var sendData={
                   account:'usr1',
                   passwd:'123456'
                  }
            $.ajax({
                url: "http://localhost:8080/API/Login",
                type: "post", //请求方式为post
                data:sendData,
                dataType: "json", //返回数据格式为json
                success: function (data) { //请求成功完成后要执行的方法
				//假如 服务端返回 {"errCode":0,"errMessage":"帐号或密码错误","companyUUID":null}
				//则下面弹出 帐号或密码错误
					alert(data.errMessage);
                }
            });
        });
    })
有的人可能没注意,返回报错from origin 'null' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource
这原因是因为html跨域的问题,要在同一域里面 如 http://localhost:8080 请求与接收端都要在这个域里面,否则会报上面那样的错误
转载请保留 http://www.luofenming.com/show.aspx?id=ART2019111300001