Jquery Ajax 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 jquery ajax handle 400 policies of this site About Us Learn more about Stack Overflow the post 400 bad request angularjs company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users
400 Bad Request Ajax Spring Mvc
Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes
400 Bad Request Post Method
a minute: Sign up jQuery.ajax returns 400 Bad Request up vote 11 down vote favorite 3 This works fine: jQuery('#my_get_related_keywords').click(function() { if (jQuery('#my_keyword').val() == '') return false; jQuery.getJSON("http://boss.yahooapis.com/ysearch/web/v1/" +jQuery('#my_keyword').val()+"?" +"appid=myAppID" +"&lang=en" +"&format=json" +"&count=50" +"&view=keyterms" +"&callback=?", function (data) {//do something} This returns 400 Bad Request (Just a reformulation of the above jQuery using .ajax to support error handling). jQuery('#my_get_related_keywords').click(function() { if (jQuery('#my_keyword').val() 400 bad request post json == '') return false; jQuery('#my_loader').show(); jQuery.ajax( { url: "http://boss.yahooapis.com/ysearch/web/v1/" +jQuery('#my_keyword').val()+"?" +"appid=myAppID" +"&lang=en" +"&format=json" +"&count=50" +"&view=keyterms" +"&callback=?", success: function(data) {//do something} ajax jquery share|improve this question edited Nov 11 '10 at 17:03 Teja Kantamneni 11k94378 asked Nov 11 '10 at 16:58 Scott B 10k40110204 do you have to add method (post or get) somewhere? –Jan Nov 11 '10 at 17:02 add a comment| 3 Answers 3 active oldest votes up vote 11 down vote accepted is not correct.. so correct way is below: $('#my_get_related_keywords').click(function() { $.ajax({ type: "POST", url: "HERE PUT THE PATH OF YOUR SERVICE OR PAGE", data: '{"HERE YOU CAN PUT DATA TO PASS AT THE SERVICE"}', contentType: "application/json; charset=utf-8", dataType: "json", success: function (msg) { //do something }, error: function (errormessage) { //do something else } }); } share|improve this answer answered Nov 11 '10 at 17:05 Andrea Turri 4,65662552 Thanks for the quick help Toro. @light was correct too. –Scott B Nov 11 '10 at 17:30 add a comment| up vote 9 down vote Add this to your ajax call: contentType: "application/json; charset=utf-8", dataType:
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
The Server Responded With A Status Of 400 (bad Request)
this site About Us Learn more about Stack Overflow the company Business "networkerror: 400 bad request Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask 500 (internal server error) ajax post Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign http://stackoverflow.com/questions/4156991/jquery-ajax-returns-400-bad-request up Continually receiving 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 http://stackoverflow.com/questions/14281537/continually-receiving-400-bad-request-on-jquery-ajax-post-to-mvc-controller homelink = '<%: Url.Action("Index","Home") %>'; 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,7722932 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
Gherkin Git Go Groovy Haml Handlebars Haskell HTML HTTP Ini iOS Jade Java Javascript jQuery JSON Julia Keyman LaTeX Linux Less LOLCODE Makefile https://codedump.io/share/wmK1lmAbjXAV/1/getting-400-bad-request-error-in-jquery-ajax-post 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 - 8 months ago 1216x Javascript Question Getting 400 bad request error in Jquery bad request 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 method API",
"sub:tags": ["facebook:work", "facebook:likes"],
"sampleSize" : 10,
"values": ["science", 400 bad request "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. To emailaddress: To name: From name: Extra information in the email body (optional): Email: I am sending you the codedump of Getting 400 bad request err