怎么用ajax发送post请求

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了怎么用ajax发送post请求相关的知识,希望对你有一定的参考价值。

参考技术A function XMLHttp()
    var xmlhttp;
    if (window.XMLHttpRequest)
      // code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
      
    else
      // code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      
    return xmlhttp;

var xhr = XMLHttp();//创建对象
xhr.open("POST","demo_post.asp",true)
/*open(method,url,async)method:请求的类型;
GET 或 POST
url:文件在服务器上的位置
async:true(异步)或 false(同步)*/
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("fname=Bill&lname=Gates");
xmlhttp.onreadystatechange=function()
  
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    
    document.getElementById("myDiv").innerhtml=xmlhttp.responseText;
    
  

参考技术B

在jQuery中有封装好的方法。如:

$.post("demo_ajax_gethint.asp",suggest:txt,function(result)
    $("span").html(result);
  );

如果是原型js的方法,那就稍微繁琐一点。

本回答被提问者采纳

nodejs post请求 怎么用url

参考技术A router: post '/test'
controller: req.body

以上是关于怎么用ajax发送post请求的主要内容,如果未能解决你的问题,请参考以下文章

jquery跨域发送Post请求该怎么处理

前端ajax中运用post请求和get请求之于session验证

如何用ajax发送post请求?

用request作post请求,需要有key值重复的form表单怎么做

怎么用javascript发送post请求?

为啥用ajax发送post请求时,需要设置请求头类型为application/x-www-form-urlencoded