JavaScript怎么post

JavaScript06

JavaScript怎么post,第1张

使用AngularJS通过POST传递参数还是需要设置一些东西才可以!

1、不能直接使用params

例如:

[javascript] view plain copy

$http({

method: "POST",

url: "http://192.168.2.2:8080/setId",

params: {

cellphoneId: "b373fed6be325f7"

}

}).success()

当你这样写的时候它会把id写到url后面:

[javascript] view plain copy

http://192.168.2.2:8080/setId?cellphoneId=b373fed6be325f7"

会在url后面添加"?cellphoneId=b373fed6be325f7",查了些资料发现params这个参数是用在GET请求中的,而POST/PUT/PATCH就需要使用data来传递

2、直接使用data

[javascript] view plain copy

$http({

method: "POST",

url: "http://192.168.2.2:8080/setId",

data: {

cellphoneId: "b373fed6be325f7"

} }).success()

这样的话传递的,是存在于Request Payload中,后端无法获取到参数

这时发现Content-Type:application/jsoncharset=UTF-8,而POST表单请求提交时,使用的Content-Type是application/x-www-form-urlencoded,所以需要把Content-Type修改下!

3、修改Content-Type

[javascript] view plain copy

$http({

method: "POST",

url: "http://192.168.2.2:8080/setId",

data: {cellphoneId: "b373fed6be325f7"},

headers: { 'Content-Type': 'application/x-www-form-urlencoded' }

}).success()

这时数据是放到了Form Data中但是发现是以对象的形式存在,所以需要进行序列化!

4、对参数进行序列化

[html] view plain copy

$http({

method: "POST",

url: "http://192.168.2.2:8080/setId",

data: {cellphoneId: "b373fed6be325f7"},

headers: { 'Content-Type': 'application/x-www-form-urlencoded' },

transformRequest: function(obj) {

var str = []

for (var s in obj) {

str.push(encodeURIComponent(s) + "=" + encodeURIComponent(obj[s]))

}

return str.join("&")

}

}).success()

下面是用js动态创建form然后进行post提交的方式,参数url是表单提交的去向,参数params是给去向传递的参数(JSON结构),参数target设置何处打开链接文档(可选):

function postcall( url, params, target){

var tempform = document.createElement("form")

tempform.action = url

tempform.method = "post"

tempform.style.display="none"

if(target) {

tempform.target = target

}

for (var x in params) {

var opt = document.createElement("input")

opt.name = x

opt.value = params[x]

tempform.appendChild(opt)

}

var opt = document.createElement("input")

opt.type = "submit"

tempform.appendChild(opt)

document.body.appendChild(tempform)

tempform.submit()

document.body.removeChild(tempform)

}

1、传统方式,判断浏览器进行post请求

var xmlobj //定义XMLHttpRequest对象

function CreateXMLHttpRequest()

{

if(window.ActiveXObject)

//如果当前浏览器支持Active Xobject,则创建ActiveXObject对象

{

  //xmlobj = new ActiveXObject("Microsoft.XMLHTTP")

  try {

      xmlobj = new ActiveXObject("Msxml2.XMLHTTP")

      } catch (e) {

     try {

       xmlobj = new ActiveXObject("Microsoft.XMLHTTP")

        } catch (E) {

          xmlobj = false

         }

        }

       }

else if(window.XMLHttpRequest)

//如果当前浏览器支持XMLHttp Request,则创建XMLHttpRequest对象

{

  xmlobj = new XMLHttpRequest()

}

}

function SubmitArticle(act,cityname,antique) //主程序函数

{

  CreateXMLHttpRequest() //创建对象

  //var parm = "act=firstweather" //构造URL参数

  //antique = escape(antique)

  var parm = "act=" + act + "&cityname=" + cityname + "&antique=" + antique//构造URL参数

  //xmlobj.open("POST", "{dede:global.cfg_templeturl/}/../include/weather.php", true) //调用weather.php

  xmlobj.open("POST", "/weather/include/weather.php", true) //调用weather.php

  xmlobj.setRequestHeader("cache-control","no-cache")

  xmlobj.setRequestHeader("contentType","text/htmlcharset=uft-8") //指定发送的编码

  xmlobj.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")  //设置请求头信息

  xmlobj.onreadystatechange = StatHandler  //判断URL调用的状态值并处理

  xmlobj.send(parm) //设置为发送给服务器数据

}

2、虚拟表单的形式提交post请求

function post(URL, PARAMS) {

  var temp = document.createElement("form")

  temp.action = URL

  temp.method = "post"

  temp.style.display = "none"

  for (var x in PARAMS) {

    var opt = document.createElement("textarea")

    opt.name = x

    opt.value = PARAMS[x]

    // alert(opt.name)

    temp.appendChild(opt)

  }

  document.body.appendChild(temp)

  temp.submit()

  return temp

}

//调用示例:

post('请求地址', {html :prnhtml,cm1:'sdsddsd',cm2:'haha'})