简体   繁体   中英

Posting a JSon object to Webservice via AJAX

I'm trying to send an object in JSon format to my Java backend by AJAX, but I was unsuccessful. I wonder if the syntax is properly correct.

 <!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script src="scripts/jquery.min.js.download"></script> <script src="scripts/jquery.imagemapster.js.download"></script> <script> $(document).ready(function(){ $("button").click(function(){ var myJSon = {"name":"Jo","age":30,"city":"Ny"}; $.ajax({ type: "POST", url: 'http://localhost:8080/Servidor/server', //contentType: "application/json;charset=utf-8", dataType: "json", data: JSON.stringify(myJSon), success: function (data){ alert('Sucess'); }, error: function () { alert('Error'); } }); }); }); </script> </head> <body> <button>Send an HTTP POST request to a page and get the result back</button> </body> </html> 

When the line contentType: "application/json;charset=utf-8" is not commented out, I get the following error in the backend: INFO: Could not find grammar element for class java.lang.String

Is the syntax correct? Can the error be from the backend itself?

PS: Sorry for my bad english

Your code is good, you just misspelled "success" on the AJAX call (you need 2 letter c's instead of 1). So replace sucess: function(data){...} with success: function(data){...} https://jsfiddle.net/stephentillman/aow5pah2/

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