Jquery Ajax Post 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 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 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", "facebook:likes"],
400 Bad Request Post Method
"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.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 Nam
here for a quick overview of the site Help Center Detailed answers
400 Bad Request Post Json
to any questions you might have Meta Discuss the workings http post bad request and policies of 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 Question x Dismiss Join the Stack http://stackoverflow.com/questions/16017081/getting-400-bad-request-error-in-jquery-ajax-post 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 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()+"?" http://stackoverflow.com/questions/4156991/jquery-ajax-returns-400-bad-request +"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() == '') 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: functi
Gherkin Git Go Groovy Haml Handlebars Haskell HTML HTTP Ini iOS Jade Java Javascript https://codedump.io/share/wmK1lmAbjXAV/1/getting-400-bad-request-error-in-jquery-ajax-post jQuery JSON Julia Keyman LaTeX Linux Less LOLCODE Makefile http://sharepoint.stackexchange.com/questions/123642/ajax-odata-400-bad-request 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 bad request markup YAML Other Search blunderboy - 8 months ago 1219x 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',
400 bad request 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 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-requ
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 about Stack Overflow the company Business Learn more about hiring developers or posting ads with us SharePoint Questions Tags Users Badges Unanswered Ask Question _ SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute: Sign up Here's how it works: Anybody can ask a question Anybody can answer The best answers are voted up and rise to the top Ajax Odata 400 bad request up vote 1 down vote favorite I get a 400 Bad Request from this code, hard to debug, what can the problem be? function saveSelectedAnswer(radioValue) { var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_vti_bin/listdata.svc/PollUserData"; var listItems = { 'Question': allQuestionsArray[0].Question, 'Answer': radioValue, 'QuestionID': allQuestionsArray[0].ID, 'UserID': userGuid }; var requestBody = JSON.stringify(listItems); var requestHeaders = { "ACCEPT": "application/json;odata=verbose" } $.ajax({ url: requestUri, type: "Post", processData: false, contentType: "application/json;odata=verbose", data: requestBody, headers: requestHeaders, success: function(data) { console.log("data saved"); }, error: function(xhr, thrownError) { alert(xhr.status + " " + thrownError); } }); } jquery ajax odata share|improve this question asked Dec 9 '14 at 15:40 Anders 4742718 Do you have a list with title PollUserData? –Amal Hashim Dec 9 '14 at 15:43 Try to open http://siteurl/_vti_bin/listdata.svc and check you can see the Mentioned list –Amal Hashim Dec 9 '14 at 15:44 add a comment| 3 Answers 3 active oldest votes up vote 0 down vote Check if you can see the list with a basic get request with something like: $.getJSON("_spPageContextInfo.webAbsoluteUrl + "/_vti_bin/listdata.svc/PollUserData", function(data) { console.log(data) }); Which will log the contents to your browser dev tools console. Once you get that figured out.... You're also not passing the request digest in your post request. The request digest acts as an anti-forgery token and is required for all POST operations to SP. This isn't too big of a problem though, if you're on a SharePoint page you there is a hidden form element containing this information -- it has the ID: __REQUESTDIGEST Change your headers object to: headers: { "Accept"