POST请求(Javascript) [英] POST Request (Javascript)

查看:79
本文介绍了POST请求(Javascript)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

虽然我很喜欢,但我不知道如何使用Javascript来发布POST请求。从@sundeep回答代码示例,但在此处发布代码以获得完整性

  var url =sample-url.php ; 
var params =lorem = ipsum& name = alpha;
var xhr = new XMLHttpRequest();
xhr.open(POST,url,true);

//发送正确的头信息和请求
xhr.setRequestHeader(Content-type,application / x-www-form-urlencoded);

xhr.send(params);


How do you make a simple POST request in Javascript without using a forms and without posting back?

解决方案

Though I am taking the code sample from @sundeep answer, but posting the code here for completeness

var url = "sample-url.php";
var params = "lorem=ipsum&name=alpha";
var xhr = new XMLHttpRequest();
xhr.open("POST", url, true);

//Send the proper header information along with the request
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

xhr.send(params);

这篇关于POST请求(Javascript)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆