我正在尝试将我的用户输入作为JSON对象添加到localHost网站。这是我的HTML页面。当用户单击“添加服务”时,该按钮应将信息添加为JSON对象。
HTML页面。
<table style="width:100%" id= "intake">
<tr><td>
<label for="firstName">First Name: </label></td><td>
<input type="text" name="firstName" id="firstName" v-model="firstName"></td> </tr>
<tr><td>
<label for="lastName">Last Name: </label></td><td>
<input type="text" name="lastName" id="lastName" v-model="lastName"></td> </tr>
<tr><td>
<label for="member">Member Number:</label></td><td>
<input type="number" name="member" id="member" v-model="member" min= "1"> </td> </tr>
<tr><td>
<label for="ticket">Ticket Number:</label></td><td>
<input type="number" name="ticket" id="ticket" v-model="ticket" min= "1"> </td> </tr>
<tr><td>
<label for="phone">Phone Number:</label> </td><td>
<input type="number" name="phone" id="phone" v-model="phone" min= "1"> </td> </tr>
<tr><td>
<label for="email">Email:</label> </td><td>
<input type="email" name="email" id="email" v-model="email" > </td> </tr>
<tr><td>
<label for="services">Service:</label> </td><td>
<select name="services" id="services" v-model="services">
<option value = "0">Options...</option>
<option value = "1">Bronze Tune</option>
<option value = "2">Silver Tune</option>
<option value = "3">Gold Tune</option>
<option value = "4">Bronze Wax</option>
<option value = "5">Silver Wax</option>
<option value = "6">Wax</option>
</select> </td> </tr>
<tr><td>
<label for="notificationPreference">Notification Preference</label> </td><td>
<select name="notification" id="notification" v-model="notification">
<option value = "0">Options...</option>
<option value = "1">1</option>
<option value = "2">2</option>
<option value = "3">3</option>
</select> </td> </tr>
<tr><td>
<button class = "button5" id="add_service">Add Service</button>
</td></tr>
<tr><td>
<label for="notes">Notes:</label> </td><td>
<textarea type="notes" name="notes" id="notes" v-model="notes" input style="height:200px"></textarea> </td> </tr>
</table>
</div>
</form>
并非所有这些选项都会被添加;仅添加名字,姓氏,电话号码,电子邮件和通知首选。
这是我必须添加的JS脚本。
function addCustomer() {
document.getElementById('add_service').onclick = function() {
var request = new XMLHttpRequest();
var firstName = document.getElementById("firstName").value;
var lastName = document.getElementById("lastName").value;
var phoneNum = document.getElementById("phone").value;
var emailAddress = document.getElementById("email").value;
var notifications = document.getElementById("notification").value;
request.open('GET', url, true);
request.onload = function () {
var url = 'localhost:8080/customer/add?
firstName='firstName'
&lastName='lastName'
&custPhone='phoneNum'
&custEmail='emailAddress'
¬ificationPreference='notifications;
}
request.send();
};
}
这个JS脚本的作用是 - 如果用户单击“添加服务”,它应该将此信息添加到JSON对象。
这是一个URL,当用户输入附加到该URL时,会将其添加为对象。
localhost:8080/customer/add?firstName=STRING&lastName=STRING&custPhone=STRING&custEmail=STRING¬ificationPreference=INTEGER
我在这里做错了什么?我是否将用户输入错误附加到URL上,或者错误地点击了按钮?
编辑:添加JSON对象示例
{
"custID": 100501,
"custEmail": "test1@test1.net",
"custPhone": "1115552222",
"firstName": "TestFirst1",
"lastName": "TestLast1",
"notificationPreference": 2
},
答案 0 :(得分:0)
我在这里看到的一个问题是网址中的错误连字。你错过了'+'运算符。这有效:
var firstName = 'Max';
var url = 'localhost:8080/customer/add?firstName=' + firstName + '&lastName=';
console.log(url);