我正在尝试通过 ajax 调用向我的 Web 服务发送一些值。
html
<button type="submit" id="gen" class="btn btn-primary" onclick="getval();">
Javascript
$(document).ready(function () {
function getval(){
$.ajax({
url: base_url+"/aa/testService.php",
contentType: "application/json; charset=utf-8",
dataType: "json",
type: "GET",
data: {"name" :"abc",age="20"}
success: function (response) {
console.log(response);
}
});
}});
但我的错误日志显示
line 38 "data: {"name" :"abc",age="20"}" - Unexpected " :", expected one of: "}"
这是为什么?如何解决?
解决方案更新
删除data:object
并通过url传递值url: base_url+"/aa/testService.php?name='abc'"
最佳答案
data
属性中的语法错误。将 =
替换为 :
并在其后添加 ,
。
data: {"name": "abc", age: "20"},
success: function (response) {
console.log(response);
}
关于javascript - 如何通过 Ajax 将值传递给 Web 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46924511/