我应该在以下代码中进行哪些修改,以便以 json 字符串的形式将多个值从表单发布到我的 Web 服务?
我试过这个 -
<script>
function ajaxRequest(){
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if(xmlhttp.readyState == 4){
alert(xmlhttp.readyState);
}
};
var namevalue=encodeURIComponent(document.getElementById("name").value);
// var fname=encodeURIComponent(document.getElementById("lastname"));
var parameters="name="+namevalue
var url="http://localhost:41191/test/resources/postservice";
xmlhttp.open("POST",url,true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
xmlhttp.send(parameters)
}
</script>
还使用-
function ajaxRequest(){
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if(xmlhttp.readyState == 4){
alert(xmlhttp.readyState);
}
};
var parameters = { "name": $('#name').val(), "firstname": $('#fname').val() };
var url = 'http://localhost:41191/test/resources/storeincompleteform';
xmlhttp.open('POST', url, true);
xmlhttp.setRequestHeader('Content-type', 'application/json')
xmlhttp.send(JSON.stringify(parameters)); }
</script>