Bad Request error when posting data using jQuery in IE only - javascript

I've written a small single-page application using Knockout.js and Sammy.js, plus jQuery for posting data back to the server.
This works fine in Firefox, but all my POSTs are getting a "400 Bad Request" response in IE.
I've used Fiddler to see what's going on, and the only difference I can see is that IE includes the hash in the Referer header, but I don't think that should cause a problem.
Here's my jQuery post:
$.ajax({
type: 'POST',
url: '/App_Presentation/BDM/RdmUserCategoryService.svc/GetUserCategoryUsersByCategory',
data: '{ "categoryId" : "' + this.params.catId + '" }',
contentType: 'application/json; charset=utf=8',
dataType: 'json',
success: function (data) {
self.editCategoryData({
CategoryID: ko.observable(categoryId),
CategoryName: ko.observable(categoryName),
UserList: ko.observableArray([])
});
self.editCategoryData().UserList(data.d);
}});
The method on the server doesn't get hit, and the success callback doesn't get hit either. When I add an error callback, the only information I get is the "Bad Request" error message.
Has anyone got any ideas?

So anyone else viewing this may try a solution to this problem. Try removing charset from your contentType. It worked for the problem above.
It could have to do with slight encoding differences between the two browsers, WCF may reject the incoming request given the encoding of the request doesn't actually match the encoding specified in the contentType. While this isn't a sure fire solid answer. It's the best I have.
Final Code:
$.ajax({
type: 'POST',
url: '/App_Presentation/BDM/RdmUserCategoryService.svc/GetUserCategoryUsersByCategory',
data: '{ "categoryId" : "' + this.params.catId + '" }',
contentType: 'application/json'
dataType: 'json',
success: function (data) {
self.editCategoryData({
CategoryID: ko.observable(categoryId),
CategoryName: ko.observable(categoryName),
UserList: ko.observableArray([])
});
self.editCategoryData().UserList(data.d);
}});

Related

Jquery: Probably a syntax Issue in ajax() method - Value not getting sent

I'm able to dump value of the variable message in console .
But im not able to send it off in POST Request.
AJAX call:
chat.throwmsg = function(message) {
if ($.trim(message).length != 0) {
console.log(message);
$.ajax({
url: 'ajax/chat.php',
type: 'post',
data: { method: 'throw', message: message} ,
success: function(data) {
chat.fetchmsgs();
$('textarea#entry').val('');
}
});
}
}
This maybe due to wrong syntax, but I've tried both single and double quotes, and combination as well .
With a wild assumption, you are not having any error messages in developer console and chat.php has a post handler forthese parameters
Since your data is JSON, please change the code as this way and have a try..
var temp={ method: 'throw', message: message};
var param=JSON.stringify(temp);
$.ajax({
url: 'ajax/chat.php',
type: 'post',
data: param ,
dataType: "json",
success: function(data) {
chat.fetchmsgs();
$('textarea#entry').val('');
}
});
after reviewing the code I could not find any issues that restrict the data to be sent along with ajax request,if you are having any syntax errors you should have been warned prior the request initialization, however I recommend you to check the request header in the network tab of browser console and see your sending data along with the request if it's there you probably need to check the code of getting the post data in your server-side implementations

Ajax data sending format

Is my Ajax format something wrong?
I am trying to send a simple digit 1/0 data to a tomcat server, using ajax
but getting no response. The whole view is at a standstill.
$.ajax({
type: "GET",
cache: false,
async: false,
url: "http://192.168.200.163:8080/ControllerWebServer/mainCTRL.jsp",
data: “fan=” + fan,
success: function(fan){
alert(fan);
},
error: function(){
alert("Ajax Error.");
}
});
If I make this ajax block as a comment, other functions (which is not displayed here) work just well. I tried GET AND POST, and just the same: don't work.
please help... :(
Try changing this
data: “fan=” + fan,
to
data: {fan: fan},
This is how data should be formatted.
sorry i have low reputation so i couldnt make comment.
when you using data: {fan: fan}, you need to specify the dataType:'json' in your ajax

Not able to get data that is in Bengali language from server using jQuery AJAX

I am not able to get data that is in Bengali language from server using ajax. The data from server is getting replaced by something I don't know. But if I normally set the data means not using ajax, data is getting displayed properly. My code is:
<meta content="html/text" charset="UTF-8"> in .jsp file
$.ajax({
url: "",
data: "a=a",
contentType: "charset=utf-8",
method: "get",
success:function(){
},
failure:function(){
}
});
I have taken reference from UTF8 encoding not working when using ajax, but even this did not work.
The error is in the ajax statement config.
contentType="charset=utf-8",
Here, you should use : instead of =.
See the error highlighted in the code below.
$.ajax({
url: "YOUR URL HERE?myparam1=value1&param2=value2",
data: "a=a",
contentType: "charset=utf-8",
// ^
method: "GET",
success: function(resp) {},
failure: function(err) {}
});

Ajax cross domain POST in IE8 and IE9

I am trying to make a ajax cross domain POST in IE9 and IE8 to a rest endpoint that resets a password and am not having any luck. The ajax call I am trying to make is as follows:
$.ajax({
type: 'POST',
crossDomain: true,
url: postUrl,
contentType: 'application/json; charset=utf-8',
data:{ "userid":uid, "email":email, "password":password },
dataType: 'jsonp',
success: function (data) {
console.log(data);
},
error: function (status){
console.log(status);
}
});
I am also making sure to include the following before the POST call:
$.support.cors = true;
Everytime I make the call it goes into the error function. It says the status is success however when I try to login with the new password it is not working which means it wasnt a success. The following is the response that I output to the log:
[object Object]{readyState: 4, status: 200, statusText: "success"}
Also note that I am calling the jQuery.XDomainRequest.js by MoonScript. That several other stack overflow answers say to do. And my jquery version is 1.11.3
Can someone please provide me some direction on this.

Amplifyjs GET request with body

I'd like to be able to use GET with message-body using AmplifyJS.
My question is specifically about how to achieve that with AmplifyJS.
Synthetic tests work fine (Fiddler being my test client). But when I do it through AmplifyJS, the "body" is added to the query string. (Edit: Is this a limitation of underlying jQuery "ajax" or that's AmplifyJS's choice)
amplify.request.define('trickyGet', 'ajax', {
url: 'entity/{param}',
dataType: 'json',
type: 'GET',
contentType: '???'
});
then
amplify.request({
resourceId: 'trickyGet',
data: { param: 'paramVal', data: JSON.stringify({'complex1': 'complex1Val'}) },
//data: { param: 'paramVal', data: {'complex1': 'complex1Val'}) },
success: callbacks.success,
error: callbacks.error
});
I tried all kinds of combinations with how my complex data is represented and what content-type to use.
If I change type to POST (and of course change my API end point), everything works (data is in the message-body). But with get, data is appended to query string (by AplifyJS).
amplify.js uses jQuery's ajax() method, which in turn limits how data is sent during get requests.

Categories

Resources