简体   繁体   English

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

[英]How to pass values to web service via Ajax

I'm trying to send some values to my web service on ajax call. 我正在尝试通过ajax调用向我的Web服务发送一些值。

html HTML

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

Javascript 使用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);
        }
  });
}});

but my error log showing 但是我的错误日志显示

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

why is that?how to resolve this? 为什么?如何解决?

Solution-update 解决方案更新

remove data:object and pass values through url url: base_url+"/aa/testService.php?name='abc'" 删除data:object并通过url url: base_url+"/aa/testService.php?name='abc'"传递值url: base_url+"/aa/testService.php?name='abc'"

You have a wrong syntax in the data property. 您在data属性中使用了错误的语法。 Replace the = with : and add , after it. 替换=:添加,之后它。

data: {"name": "abc", age: "20"},
success: function (response) {
   console.log(response);
}

Please follow the solution below. 请按照下面的解决方案。

<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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM