Http Error 400 Ajax
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 of this site About Us Learn more 400 bad request in ajax call about Stack Overflow the company Business Learn more about hiring developers or posting ads
Jquery Ajax Handle 400
with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow post 400 bad request angularjs is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Getting 400 bad request error in Jquery Ajax POST up vote 23
400 Bad Request Ajax Spring Mvc
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", "facebook:likes"], "sampleSize" : 10, "values": ["science", "machine-learning"] }, error: function(e) { console.log(e); } }); It Says: Can not build resource from request. What am post 400 error 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.8k1158104 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 43 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: 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. share|improve this answer edited Apr 17 '13 at 9:58 a
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 Method
about Stack Overflow the company Business Learn more about hiring developers or posting ads
400 Bad Request Post Json
with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack http post bad request 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 400 (Bad Request) on jquery ajax post to MVC controller http://stackoverflow.com/questions/16017081/getting-400-bad-request-error-in-jquery-ajax-post 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") %>'; window.location.href = (homelink); returndata = data; }, error: function (xhRequest, ErrorText, thrownError) { alert("Failed http://stackoverflow.com/questions/14281537/continually-receiving-400-bad-request-on-jquery-ajax-post-to-mvc-controller 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 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, use
Extensions Demos Logo Documentation Take the Tour Tutorials Class Reference Wiki Guide 2.0 Class Reference 2.0 Screencasts Resources Development Contribute to Yii Latest Updates Report a http://www.yiiframework.com/forum/index.php/topic/35874-ajax-post-return-error-400-bad-request/ Bug Report a Security Issue Community Forum Live Chat News Hall of Fame Badges search Yii Framework Forum: $.ajax Post Return Error 400 Bad Request - Yii Framework Forum https://bugs.jquery.com/ticket/9613 Jump to content Sign In » New user? Register Now! Help Search Advanced ForumsMembers Yii Framework Forum > Yii 1.1.x > General Discussion View New Content Page 1 of bad request 1 You cannot start a new topic You cannot reply to this topic $.ajax Post Return Error 400 Bad Request Rate Topic: #1 freshuser Junior Member Group: Members Posts: 52 Joined: 19-June 12 Posted 24 September 2012 - 03:00 AM Hi, I have this controller function generated by gii public function actionDelete($id) { if(Yii::app()->request->isPostRequest) { // we 400 bad request only allow deletion via POST request $this->loadModel($id)->delete(); // if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser if(!isset($_GET['ajax'])) $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin')); } else throw new CHttpException(400,'Invalid request. Please do not repeat this request again.'); } I need to call this function in javascript and my js code is $.ajax ({ url: "/people/delete", type: "post", data: { YII_CSRF_TOKEN: $("#YiiCsrf").val(), id: $(this).attr("name") }, success: function(response, textStatus, jqXHR) { alert(response); }, error: function(jqXHR, textStatus, errorThrown) { alert(jqXHR.status + ' ' + textStatus + ' - ' + errorThrown); }, complete: function() { } }); I got error 400 Bad Request. Anything wrong? 0 Back to top of the page up there ^ #2 Keith Elite Member Group: Moderators Posts: 1,707 Joined: 04-March 10 Location:UK Posted 24 September 2012 - 04:17 AM I may be completely wrong, but I believe only GET data is passed into the action's parameters by default. This thread may point you in the right direction: http://www.yiiframew...ameter-binding/ 0 Back to top of
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