Ajax POST returning an Bad Request 400 -


i have been trying few hours debug post ajax call server.

i have 2 post methods: helloworld , helloyou.

same code, difference helloyou takes string parameter:

namespace webservice {     [servicecontract(namespace = "")]     [aspnetcompatibilityrequirements(requirementsmode = aspnetcompatibilityrequirementsmode.allowed)]     public class service     {         [operationcontract]         [webinvoke(method = "post",             bodystyle = webmessagebodystyle.wrappedrequest,             responseformat = webmessageformat.json,             requestformat = webmessageformat.json)]         public string helloworld()         {             return "hello world";         }          [operationcontract]         [webinvoke(method = "post",             bodystyle = webmessagebodystyle.wrappedrequest,             responseformat = webmessageformat.json,             requestformat = webmessageformat.json)]         public string helloyou(string name)         {             return string.format("hello {0}",name);         }     } } 

the html client looks that:

<!doctype html> <html xmlns="http://www.w3.org/1999/xhtml"> <head>     <title>my application</title>     <script type="text/javascript" src="/scripts/jquery-2.1.1.min.js"</script>     <script type="text/javascript" src="/scripts/ajax.js"> </script>     <script type="text/javascript" src="/scripts/events.js"> </script> </head> <body>     <button id="world">helloworld</button>     <button id="you">helloyou</button> </body> </html> 

and ajax calls:

$(document).ready(function () {     $('#world').click(function () {         helloworld();     });      $('#you').click(function () {         helloyou();     }); });  baseaddress = "http://localhost:53016/service.svc/ajax/";  function geturl(method) {     return baseaddress + method; }  function helloworld() {     $.ajax({             async: false,             url: geturl("helloworld"),             datatype: 'json',             type: 'post',             data: null ,             processdata: true,             contenttype: "application/json;charset-uf8"         })          .done(function(data) {             alert(data.d);         })         .fail(function (xhr, status, errorthrown) {             alert(status + errorthrown);         }); }  function helloyou() {     $.ajax({         async: false,         url: geturl("helloyou"),         datatype: 'json',         type: 'post',         data: json.stringify('{"name": "chris"}'),         processdata: true,         contenttype: "application/json;charset-uf8"     })     .done(function (data) {         alert(data.d);     })         .fail(function (xhr, status, errorthrown) {         alert(status + errorthrown);     }); } 

i have tried few different ways pass parameter ajax call:

data: json.stringify('{"name": "chris"}'), data: '{"name": "chris"}', data: '{name: "chris"}',  var name ="chris" data: '{name: ' + json.stringify(name) + '}', 

every time, error bad request 400. same function helloworld no parameter works fine.

i lost.

i checked fidler html request/response:

post /service.svc/ajax/helloyou http/1.1

http/1.1 400 bad request

thanks all

isidore

i found way make work. changed method post get.

for data, looked on jquery api documentation:

    data: { name: "john"}, 

and works. surprised, thought wouldn't let me push data server.

cheers

isidore


Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -