Jquery Ajax Bad Request Error
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies
Post 400 Bad Request Angularjs
of this site About Us Learn more about Stack Overflow the company jquery ajax handle 400 Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges 400 bad request ajax spring mvc 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 a
Post 400 Error
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 Method
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: "json" share|improve this answer answer
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 site About Us Learn more 400 bad request post json about Stack Overflow the company Business Learn more about hiring developers or posting ads
Http Post Bad Request
with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow "networkerror: 400 bad request is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Continually receiving 400 (Bad Request) on jquery ajax post to MVC controller up http://stackoverflow.com/questions/4156991/jquery-ajax-returns-400-bad-request 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") %>'; window.location.href = (homelink); returndata = data; }, error: function (xhRequest, ErrorText, thrownError) { alert("Failed to http://stackoverflow.com/questions/14281537/continually-receiving-400-bad-request-on-jquery-ajax-post-to-mvc-controller 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 here's the correct way to do the request: $.ajax({ //actually approve or reject the promotion url: url, type: "POST", data: JSON.stringify({ // Those property names must match the property names of your PromotionDecision view model promotionId: data.PromotionId, userId: data.UserId, reasonText:
Gherkin Git Go Groovy Haml Handlebars Haskell HTML HTTP Ini iOS Jade Java Javascript jQuery JSON Julia Keyman LaTeX Linux Less LOLCODE Makefile Markdown MATLAB MySQL NASM Node.js NSIS Objective-C https://codedump.io/share/wmK1lmAbjXAV/1/getting-400-bad-request-error-in-jquery-ajax-post Pascal Perl PHP PHP Extras PowerShell Python R React JSX reST (reStructuredText) https://bugs.jquery.com/ticket/9613 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 1214x Javascript Question Getting 400 bad request error in Jquery Ajax POST I am trying to send an Ajax POST request using Jquery bad request 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", "machine-learning"]
},
error: function(e) {
console.log(e);
}
});
It Says: Can not build resource 400 bad request 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 error in Jquery Ajax POST that you can see here: https://codedump.io/share/wmK1lmAbjXAV/1 Close Send email Share Sign up Sign up with GitHub Email: Displayname Password: Repeat password: Tags jquery ajax post http-status-code-400 Latest added ReCAPTCHA In Laravel 5.2 Forms For Validation f
Navigation ← Previous TicketNext Ticket → Opened 5 years ago Closed 5 years ago Last modified 5 years ago #9613 closed bug (invalid) $.ajax is not launching error callback on 400 Bad Request when jsonp request is made Reported by: Somebi Owned by: Priority: low Milestone: 1.next Component: ajax Version: 1.6.1 Keywords: neededdocs Cc: jaubourg Blocked by: Blocking: Description Here is sample http://jsfiddle.net/pLuPb/ Here is working sample: http://jsfiddle.net/XVGH9/1/ Thanks ;) Oldest first Newest first Threaded Comments only Change History (3) comment:1 Changed 5 years ago by timmywil Cc jaubourg added Component changed from unfiled to ajax Priority changed from undecided to low Status changed from new to open Confirmed. Can we detect 400s with jsonp tho? comment:2 Changed 5 years ago by jaubourg Keywords needsdocs added Resolution set to invalid Status changed from open to closed What I don't understand is why the doc says "Note: Prior to jQuery 1.5, this handler was not called for cross-domain script and JSONP requests" while the correct sentence is "Note: this handler IS not called for cross-domain script and JSONP requests" ;) So, yes, the answer is no. jQuery doesn't detect HTTP errors for cross-domain script and jsonp requests as is (should be) documented. comment:3 Changed 5 years ago by addyosmani Keywords neededdocs added; needsdocs removed Docs updated: http://api.jquery.com/jQuery.ajax/ Note: See TracTickets for help on using tickets. Download in other formats: Comma-delimited Text Tab-delimited Text RSS Feed Context Navigation Next Ticket → Copyright 2013 The jQuery Foundation Hosting provided by Media Temple CDN provided by MaxCDN Download Documentation Tutorials Bug Tracker Discussion