angular js jquery中post请求的一点小区别

这也是最近遇到的坑,还是之前那个项目,现在要实现登录功能。

背景:注册功能之前已经跑通了。前端用的是jquery后台是springMVC。鉴于注册和登录有些接口功能是类似的(比如注册确保邮箱是没有注册过,而登录是确保注册过),于是后台还准备用注册的那套接口。

登录的接口get请求是没问题的,但是post却出了问题:后台收不到请求体里的内容。

后来发现是jquery和angular的post行为有些区别,于是我做了个实验。

<!DOCTYPE html>
<html lang="en" ng-app="app">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body ng-controller="body">
<button id="ng-btn" ng-click="sendNgPost()">ng-btn</button>
<button id="jq-btn">jq-btn</button>
<button id="js-btn">js-btn</button>
</body>
<script src="bower_components/jquery/dist/jquery.js"></script>
<script src="bower_components/angular/angular.js"></script>
<script>
angular.module(‘app‘,[]).controller(‘body‘,function ($scope,$http) {
    var data={username:‘zhangdongming‘,password:‘123123123‘};
    $scope.sendNgPost=function () {
        $http({
            method:‘POST‘,
            url:‘/fromNg‘,
            data:data
        }).then(function (res) {
            console.log(res);
        })
    };
    $(‘#jq-btn‘).click(function () {
        $.post(‘/fromJq‘,data,function (data) {
            console.log(data);
        })
    });
    function post(sendData) {
        var xhr=new XMLHttpRequest();
        xhr.open(‘post‘,‘/fromJs‘,true);
        xhr.onload=function () {
            console.log(xhr.responseText);
        };
        xhr.send(data);
    }
    var btn=document.querySelector(‘#js-btn‘);
    btn.onclick=function () {
        post(data);
    }
});

</script>
</html>

这段代码的作用就是用angularjs,jquery和js发post请求

服务端是express写的

var express=require("express");
var mime = require(‘mime‘);
var http = require(‘http‘);
var util = require(‘util‘);
var url = require(‘url‘);
var fs = require(‘fs‘);
var path=require(‘path‘);
var formidable=require(‘formidable‘);
var querystring = require(‘querystring‘);
var bodyParser=require("body-parser");
app=express();
// var data=[
//     {screenName:"zhangdongming",phoneNumber:"15210938964",email:"[email protected]"}
// ];
app.use(express.static(path.join(__dirname)));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended:false}));
app.get(‘/‘,function (req,res) {
    res.sendfile(path.resolve("client.html"));
});
app.post(‘/fromNg‘,function (req,res) {
    console.log(req.body);
    res.send(‘false‘);
});
app.post(‘/fromJq‘,function (req,res) {
    console.log(req.body);
    res.send(‘false‘);
});
app.post(‘/fromJs‘,function (req,res) {
    console.log(req.body);
    res.send(‘false‘);
});
app.listen(3000);
注意,body-parser中间件use我写了两句。好了现在看看这三个请求是什么样子的。这个是angular的用jquery
用js

注意看Content-Type ng是appliction/json,jq是application/x-www-form-urlencoded,js是text/plain。

而Request Payload中,ng是json字符串,jq经过了序列化,js是个???

对于express的body-parse中间件来说,两种格式的都可以,只需要写好对应的use就可以了。

而我们后台的接口写法只能接收urlencoded格式的,json是收不到的。

这种事情自然是前端想办法,很简单,都转成jquery的格式就行了。

具体来讲,对于js来讲,加上两句:

1.换格式config(function ($httpProvider) {

    $httpProvider.defaults.headers.post[‘Content-Type‘] = ‘application/x-www-form-urlencoded;charset=utf-8‘;})

2.序列化工作就比较简单了,因为用angular一般会默认带上jqeruy,$.param()可以方便的完成这项工作

$http({

    method: ‘POST‘,    url: ‘/fromNg‘,    data: $.param(data)}).then(function (res) {    console.log(res);})

以上。
时间: 2024-12-11 14:54:00

angular js jquery中post请求的一点小区别的相关文章

js jQuery中Ajax请求参数转义问题

前几天在工作的时候,在和移动端做接口的时候发现,前端在传递参数的时候其中有+或者&时候,服务器获得串中+或者&都变成了空格,后台Java程序在解析的时候出错了! 具体代码如下: $(function () { var isPc = !(window.__detect.android||window.__detect.ios||window.__detect.phone), moonCakeNums, moonCakeIdentify= 0, <span style="col

[JS]jQuery中attr和prop方法的区别

相比attr,prop是1.6.1才新出来的,两者从中文意思理解,都是获取/设置属性的方法(attributes和properties).只是,window或document中使用.attr()方法在jQuery1.6之前不能正常运行,因为window和document中不能有attributes.prop应运而生了. 之前看网上对比两者的文章,更是列出一个表来区分什么标签下使用prop,什么标签下使用attr,原谅我是懒惰的人,最害怕要背的东西,所以只有自己想想办法了. 既然我们想知道他们两的

Angular和jQuery的ajax请求的差别

近期项目中使用angular,结果发现后台没法获取參数,所以,略微研究了一下两者在发送ajax时的差别. 注意angular和jquery的ajax请求是不同的. 在jquery中,官方文档解释contentType默认是 application/x-www-form-urlencoded; charset=UTF-8 contentType (default: 'application/x-www-form-urlencoded; charset=UTF-8') Type: String Wh

Angular和jQuery的ajax请求的区别

最近项目中使用angular,结果发现后台没法获取参数,所以,稍微研究了一下两者在发送ajax时的区别. 注意angular和jquery的ajax请求是不同的. 在jquery中,官方文档解释contentType默认是 application/x-www-form-urlencoded; charset=UTF-8 contentType (default: 'application/x-www-form-urlencoded; charset=UTF-8') Type: String Wh

jQuery 中 ajax 请求数据应用的一个小demo

举一个jquery中ajax的应用小 demo 便于以后的更多项目拓展 ,这里要注意的是保存的文件名和文件图片路径问题 ... ajax01.html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title> ajax小例子 </title> </head> <body> <!--

paip.提升效率--数据绑定到table原理和流程Angular js jquery实现

paip.提升效率--数据绑定到table原理和流程Angular js  jquery实现 html #--keyword 1 #---原理和流程 1 #----jq实现的代码 1 #-----Angular 的实现 3 #--keyword jquery 遍历表格tr  td Angular 模板绑定 #---原理和流程 获得所有的行,第一的头行..排除,,,在的所有的删除. 遍历表格tr获得tds的所有的id数组. 根据id/id索引来获得绑定的数据源里面的数据字段..绑定到个td上..

jquery中html 与 text方法的区别

jquery中html 与 text方法的区别 24 May 2012/in 网站设计和开发 /by Bruce 接鉵jquery的时间并不长,以前都是用直接用js写的,现在发现在jquery这个框架用起来很方便,不但代码量少了,使用也比较简单,对于浏览器的兼容问题也不用担心,在使用的过程中也会遇到一些疑问,在html标签中附加子标签时所用的方法html()与text()的区别. 通常在用jquery写ajax时,都会用到html()这个方法,而不用text()这个方法,他们之间有什么区别呢?

jQuery 中的children()和 find() 的区别

<!DOCTYPE html> <html> <head> <script type="text/javascript" src="/jquery/jquery.js"></script> </head> <body> <ul class="level-1"> <li class="item-i">I</li>

jQuery中的bind() live() delegate()之间区别分析

jQuery中的bind() live() delegate()之间区别分析 首先,你得要了解我们的事件冒泡(事件传播)的概念,我先看一张图 1.bind方式 $('a').bind('click',function (){ alert('click'); }) 解析:这种方式最简单,jq扫描文档找出所有的a,让将函数绑定到每个元素的click事件上 2.live方式 $('a').live('click',function (){ alert('click'); }) 解析:jq将函数绑定到$