ajax JSON请求服务器type = POST(安全登录请求)

时间:2012-12-28 20:46:20

标签: php json

通过JavaScript提交简单 - 不是jQuery ... ajax JSON请求服务器type = POST(安全登录请求)需要Web表单吗?

如果没有网络表单怎么做...哪里放JSON发送的字符串var以及如何/什么在php中获取?

ajax3.send(jsonStringhere); // ???如何获取在php ???

function loginProcess() {
var userID = document.getElementById( "name" ).value;
var email = document.getElementById( "email" ).value;
var password = document.getElementById( "password" ).value;

ajax3 = new XMLHttpRequest();

//1st way
ajax3.open("GET","loginProcess.php?userID="+userID+"&email="+email+"&password="+password,false); 
ajax3.addEventListener("readystatechange", processResponse, true); 
ajax3.send();

changeDisplay("loginRegisterDiv");


//2nd way JSON post type here

//???

}

1 个答案:

答案 0 :(得分:2)

您不应该通过URL(GET)发送类似敏感信息。

人们经常共享网址,可能不希望隐藏其个人信息。

要模拟网络表单,请尝试发送POST请求。将JSON放在查询属性中:

var ec = window.encodeURIComponent,
    queryStr = "userID=" + ec(userID) + "&email=" + ec(email) + "&password=" + ec(password) + "&json" + ec(JSON.stringify(yourJson)),
    ajaxReq = new XMLHttpRequest();

ajaxReq.open("POST", "loginProcess.php", false);  // Should really be 'true' for asynchronous...
ajaxReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');  // Important!
ajaxReq.send(queryStr);