0

要通过纯 JavaScript 提交 - 而不是 jQuery... ajax JSON 请求到服务器类型 = 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

//???

}
4

1 回答 1

2

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

人们经常共享 URL,并且可能不希望将他们的个人信息隐藏在其中。

要模拟 Web 表单,请尝试发送 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);
于 2012-12-28T20:59:02.557 回答