如何通过Ajax将值传递给Web服务

时间:2017-10-25 05:20:05

标签: javascript ajax web-services

我正在尝试通过ajax调用向我的Web服务发送一些值。

HTML

$(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);
        }
  });
}});

的Javascript

line 38 "data: {"name" :"abc",age="20"}"  - Unexpected " :", expected one of: "}"

但我的错误日志显示

data:object

为什么?如何解决这个问题?

解决方案更新

删除url: base_url+"/aa/testService.php?name='abc'"并通过网址data

传递值

2 个答案:

答案 0 :(得分:1)

=属性中的语法错误。将:替换为,并在其后添加data: {"name": "abc", age: "20"}, success: function (response) { console.log(response); }

SELECT 
    tblReq.ReqID, 
    tblReq.ReqAmount, 
    (SELECT G_Name FROM tblGuarantors WHERE tblGuarantors.ReqID = tblReq.ReqID ORDER by G_ID ASC LIMIT 1) as G1_Name,
    (SELECT G_Name FROM tblGuarantors WHERE tblGuarantors.ReqID = tblReq.ReqID ORDER by G_ID DESC LIMIT 1) as G2_Name 
FROM tblReq

答案 1 :(得分:0)

请按照以下解决方案。

<button type="submit"  id="gen" class="btn btn-primary" onclick="getval();">


function getval(){

  $.ajax({
        url: base_url+"/aa/testService.php",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        type: "GET",
        data: JSON.stringfy({"name": "abc", "age": "20"})
        success: function (response) {                    
            console.log(response);
        }
  });

  return false;
}