Ajax Http Error 400
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and ajax error 400 bad request policies of this site About Us Learn more about Stack Overflow the company ajax http error 200 Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users ajax http error 500 Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a an ajax http error occurred minute: Sign up Getting 400 bad request error in Jquery Ajax POST up vote 23 down vote favorite 12 I am trying to send an Ajax POST request using Jquery but I am having 400 bad request error. Here is my code: $.ajax({ type: 'POST', url: "http://localhost:8080/project/server/rest/subjects", data: { "subject:title":"Test Name", "subject:description":"Creating test subject to check POST method API", "sub:tags": ["facebook:work",
An Ajax Http Error Occurred Drupal
"facebook:likes"], "sampleSize" : 10, "values": ["science", "machine-learning"] }, error: function(e) { console.log(e); } }); It Says: Can not build resource from request. What am I missing ? jquery ajax post http-status-code-400 share|improve this question edited Apr 15 '13 at 14:02 asked Apr 15 '13 at 13:57 blunderboy 10.6k1058103 Preliminary check: your browser is pointed to localhost:8080, right? –Sébastien Renauld Apr 15 '13 at 14:00 1 maybe not what is causing your issue, but it looks like ["facebook:work, facebook:likes"] should be ["facebook:work", "facebook:likes"] –smerny Apr 15 '13 at 14:01 Are you using Java and Jersey perhaps? –Henrik Andersson Apr 15 '13 at 14:01 I am using JAVA not Jersey..Sling servlet at backend. –blunderboy Apr 15 '13 at 14:02 add a comment| 2 Answers 2 active oldest votes up vote 42 down vote accepted Finally, I got the mistake and the reason was I need to stringify the JSON data I was sending. I have to set the content type and datatype in XHR object. So the correct version is here: $.ajax({ type: 'POST', url: "http://localhost:8080/project/server/rest/subjects", data: J
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this
An Ajax Http Error Occurred Http Result Code 200
site About Us Learn more about Stack Overflow the company Business Learn more an ajax http error occurred drupal 7 about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x http error 400 fix Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Continually receiving http://stackoverflow.com/questions/16017081/getting-400-bad-request-error-in-jquery-ajax-post 400 (Bad Request) on jquery ajax post to MVC controller up vote 4 down vote favorite My ajax call looks like this $.ajax({ //actually approve or reject the promotion url: url, type: "POST", data: '{'+data.PromotionId+','+data.UserId+','+data.ReasonText+'}', dataType: "json", //contentType: "application/json; charset=utf-8", success: function (data) { if (indicator == 'A') { alert('Promotion approved successfully!'); } else { alert('Promotion rejected successfully.'); } var homelink = '<%: Url.Action("Index","Home") %>'; http://stackoverflow.com/questions/14281537/continually-receiving-400-bad-request-on-jquery-ajax-post-to-mvc-controller window.location.href = (homelink); returndata = data; }, error: function (xhRequest, ErrorText, thrownError) { alert("Failed to process promotion correctly, please try again"); console.log('xhRequest: ' + xhRequest + "\n"); console.log('ErrorText: ' + ErrorText + "\n"); console.log('thrownError: ' + thrownError + "\n"); } }); And my MVC controller looks like this: [HttpPost] public HttpResponseMessage ApprovePromotion(PromotionDecision decision) { if (ModelState.IsValid && decision != null) { bool status = PromotionBo.ApprovePromotion(decision); if (status == true) return new HttpResponseMessage(HttpStatusCode.OK); } return new HttpResponseMessage(HttpStatusCode.BadRequest); } I had thought the syntax was correct on both of these however every time I make the ajax call I get a 400 response. What is it that I am doing wrong? jquery post asp.net-mvc-4 share|improve this question asked Jan 11 '13 at 15:40 Pseudonym 1,7622932 add a comment| 1 Answer 1 active oldest votes up vote 9 down vote accepted You are sending a completely broken and invalid JSON string to the server. It's normal that the controller action rejects it. In addition to that you have put into comments the contentType parameter specifying that you want to send a JSON request. So here's the correct way to do the request: $.ajax({
Gherkin Git Go Groovy Haml Handlebars Haskell HTML HTTP Ini iOS Jade Java Javascript jQuery JSON Julia Keyman LaTeX https://codedump.io/share/wmK1lmAbjXAV/1/getting-400-bad-request-error-in-jquery-ajax-post Linux Less LOLCODE Makefile Markdown MATLAB MySQL NASM Node.js NSIS Objective-C Pascal Perl PHP PHP Extras PowerShell Python R React JSX reST (reStructuredText) Rip Ruby Rust SAS Sass (Sass) Sass (Scss) Scala Scheme Smalltalk Smarty SQL Stylus Swift Twig TypeScript Vb.net VHDL Wiki markup YAML Other Search blunderboy - 7 months ago 1004x http error Javascript Question Getting 400 bad request error in Jquery Ajax POST I am trying to send an Ajax POST request using Jquery but I am having 400 bad request error. Here is my code: $.ajax({
type: 'POST',
url: "http://localhost:8080/project/server/rest/subjects",
data: {
"subject:title":"Test Name",
"subject:description":"Creating test subject to check POST ajax http error method API",
"sub:tags": ["facebook:work", "facebook:likes"],
"sampleSize" : 10,
"values": ["science", "machine-learning"]
},
error: function(e) {
console.log(e);
}
});
It Says: Can not build resource from request. What am I missing ? blunderboy Answer Email {} Share Finally, I got the mistake and the reason was I need to stringify the JSON data I was sending. I have to set the content type and datatype in XHR object. So the correct version is here: $.ajax({ type: 'POST', url: "http://localhost:8080/project/server/rest/subjects", data: JSON.stringify({ "subject:title":"Test Name", "subject:description":"Creating test subject to check POST method API", "sub:tags": ["facebook:work", "facebook:likes"], "sampleSize" : 10, "values": ["science", "machine-learning"] }), error: function(e) { console.log(e); }, dataType: "json", contentType: "application/json" }); May be it will help someone else. Source (Stackoverflow) http://stackoverflow.com/questions/16017081/getting-400-bad-request-error-in-jquery-ajax-post Comments Please enable JavaScript to view the comments powered by Disqus. × Email codedump link for Getting 400 bad request error in Jquery Ajax POST Email has been send.