开发cordova时,使用了$http的post方法,传输的参数服务器无法接收。搜索后发现,用AngularJS通过POST传输参数还是需要设置一些东西的!
1.参数不能直接使用
例如:
$http({ method: 'POST ',URL : ' http://192 . 168 . 2 . 2:8080/setId ',params 3360 { cellphoneid : ' b 373 fed 6 be 325 F7 ' })。成功();当你这样写的时候,它会在url后面写id:
http://192 . 168 . 2 . 2:8080/setId?CellphoneId=b373fed6be325f7 '将添加到url后'?CellphoneId=b373fed6be325f7 ',查了一些数据后发现GET请求中使用了params参数,需要通过数据传递POST/PUT/PATCH;
2.直接使用数据
$http({ method: 'POST ',URL : ' http://192 . 168 . 2 . 2:8080/setId ',data : { cellphoneid : ' b 373 fed6be 325 F7 ' } })。成功();这样,它就存在于请求负载中,后端无法获取参数
此时发现内容类型:应用/JSON;Charset=UTF-8,提交POST表单请求时,使用的Content-Type为application/x-www-form-URL encoded,因此需要修改Content-Type!
3.修改内容类型
$http({ method: 'POST ',URL : ' http://192 . 168 . 2 . 2:8080/setId ',data : { cellphoneid : ' b 373 fed6be 325 F7 ' },header RS : { ' Content-Type ' : ' application/x-www-form-URL encoded ' })。成功();
此时数据放入表单数据,却发现是以对象的形式存在,需要序列化!
4.序列化参数
$http({ method: 'POST ',URL : ' http://192 . 168 . 2 . 2:8080/setId ',data : { cellphoneid : ' b 373 fed6be 325 F7 ' },header RS : { ' Content-Type ' : ' application/x-www-form-URL encoded ' },transformrequest 3360 function(obj){ var strfor(obj中的var s){ str . push(encodeURIComponent)'=' encodeURIComponent(obj[s]);}返回str . join(“”);} }).成功();以上在AngularJS $http post中传输参数数据的方法是边肖分享的全部内容。希望能给大家一个参考,支持我们。