简体   繁体   中英

How to pass values to web service via Ajax

I'm trying to send some values to my web service on ajax call.

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

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'"

You have a wrong syntax in the data property. 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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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