ajax post does not send data - c#

I have this ajax-post:
$.ajax({
url: Config.Proxy + "ContentPages.aspx?t=save",
type: "POST",
contentType: "application/x-www-form-urlencoded",
data: {
content: content,
id: currentPageId,
active: "true",
subject: $('#txtSubject').val(),
webid: webid
},
success: function (data, status) {
// do something
},
error: function (xhr, desc, err) {
// Do something
}
});
I do a post to an .NET page, so I could use the debug mode to check if the data is send correctly.
I use the same code as above for a different page (other url, other data).
When I run the application and save the data I don't come in the debug mode. When I use the same code for the other page, it works fine.
I tried to compare both codes, but they are the same. I have removed some data-items, but it still does not work.
Why? Can anyone give me a hint?
Thanks
EDIT *
I know why this is happening: CKEditor. As soon as I set the variable "content" to the content of the CKEditor and use the post-call, the debug-mode is not called anymore. Also the form-keys in the code-behind are 0.
The question now is how to fix this?

Like said in the edit of my original post: Problem was caused by the content of the CKEditor. I think HTML is not parser/loaded/call-it-what-you-want in an AJAX-post.
So, with a little help (and without posting code-behind, that had nothing to do with the problem) I figured it out:
var content = escape(CKEDITOR.instances.editor1.getData());
Put the variable "content" in the data-section of the post and voila! It works like a charm.
On the receiving .NET page in code behind, decode the value:
(HttpUtility.UrlDecode(Request.Form.Get("content").NullSafeString())
Thanks anyway

Related

POST method to send RAW TEXT data and get HTML Response using Jquery AJAX Web Method and check with POSTMAN

I'm Trying to do a POST Request using RAW TEXT.
I want to send POST Request from POSTMAN like, in body Section I've selected RAW(from radio buttons) and Text(from drop-down) and pass below simple string.
01JAINAM120112356598
Now, when we click on send in Postman, Response must come in HTML string "01" on success and "00" on error.
[Type of Request & Response I Need] https://imgur.com/bC26Pw7 (In photo URL is of .php page, but I'll pass my asp.net c# page (.aspx)).
I need to use JQuery ajax code to pass request & get proper response.
Something like,
$.ajax({
url: "RFID.aspx/saveRFIDData",
type: "POST",
dataType: "text",
success: function () {
},
error: function (response) {
alert("Error")
}
});
But, when I call above URL as post method as same shown in Image above, my WebMethod in code behind is not called.
Instead, when I call same ajax call for JSON, my WebMethod is been called.
But, I don't want to pass JSON.
So, Can anyone help me to get it done with "dataType : text" & get HTML string response.
Note : If someone didn't Understand my question. Do comment first, to be more clear rather than DEVOTE this question
It seems to be "Unsupported Media Type" issue and status may be 415. Add supported media type, that might work.

asp.net api service.svc query length problems

Hello I got a service that is made for sending html mails, it will be called from a method "SendMail(data) about 1500 characters.
I access the service though JQuery with AJAX call and JSON:
var data = encodeURIComponent(JSON.stringify(dataArray));
$.ajax({
type: "GET",
url: "http://localhost:53334/Service.svc/SendMail?data=" + data,
contentType: 'application/json; charset=utf-8',
dataType: "json",
success: function () {
alert("Reklamationen er blevet sendt!");
},
error: function (err) {
alert("Kunne ikke sende reklamation! Der opstod en fejl.");
}
});
I got alot of fields and if I fill everything out it gives a 404 not found response but then if I leave some random fields empty it gets to the API and executes the method it should? So I think its about the data size. I have tried many settings in webconfig but i havent managed to find a solution.
I hope there will be someone who can help me.
You should really be using POST as indicated by Tikkes. Semantically you aren't requesting a resource from the server, so GET doesn't make sense. Also, as noted here there is a maximum length that is enforced for Urls which is what GET uses to pass params(as you can see in your code as well). POST on the other hand uses the request body to send its data. Also, as noted here POST and GET sizes are usually configurable on the server, but POST is generally much larger(2KB vs 10MB defaults) as stated in one of the answers. Hope this helps.

Model Binding to AJAX suddenly not working

I have an AJAX POST request which is supposed to send a user's search filters off to a controller action and return appropriate results. This was working perfectly, then it just stopped working. I haven't changed a thing, it was working fine, the model was binding exactly as intended, then it just broke and now it will only bind to the non-array items.
Here is my full Jquery AJAX code:
var parent = $('.recipe-search > .search-form > .filter-panel > .area');
var searchobj = {
SearchTerm: $(parent).find('input.search-terms').val(),
TimeRange:
[
$(parent).find('div.time-range').slider("values", 0),
$(parent).find('div.time-range').slider("values", 1)
],
DifficultyRange: [
$(parent).find('div.difficulty-range').slider("values", 0),
$(parent).find('div.difficulty-range').slider("values", 1)
],
Vegetarian: $(parent).find('input.vegetarian').is(':checked'),
Vegan: $(parent).find('input.vegan').is(':checked'),
DietTags: $(parent).find('input.diet-tags').val().split(' ')
};
$.ajax({
type: "POST",
url: $('.recipe-search > .search-form').attr('action'),
data: searchobj,
error: function () {
alert("There was an error getting your search results, please try again");
},
success: function (result) {
popresults(result);
}
});
Here is the C# model:
For anyone wondering the sliders are JQuery UI sliders.
In the Chrome networking tab, I can see the request gets sent off as intended
However looking at the debug watch window when a breakpoint is hit, it is clear that they are not being bound to the model correctly
I'll repeat myself, and I know some of you will call me out on this saying that I did change something, I must have, but I did not change a thing between it working exactly as intended, and it not working.
Something that does strike me as odd is that the arrays sent in the request (look at the network tab photo) appear to be sending the two values as individual properties instead of values in an array.
Well it seems that adding two lines to the AJAX code fixed this:
datatype: "json",
traditional: true
Now it works completely as intended.

Jquery Ajax requests not working on IE 10 (due to cache)

I would like to begin with this. I am fed up with IE. I have the code below:
$(function () {
$("#cal").on('click', "#forward", function () {
$.ajax({
url: "Home/Calendar?target=forward",
type: "GET",
success: function (result) {
$("#cal").html(result);
}
});
});
});
$(function () {
$("#cal").on('click', "#backwards", function () {
$.ajax({
url: "Home/Calendar?target=backwards",
type: "GET",
success: function (result) {
$("#cal").html(result);
}
});
});
});
It is an ajax call to a controller action in an C# MVC application. It just goes back and forth a calendar's months replacing the html. Now I know that you need to reattach the event due to the html() call and that is why I use on() with JQuery 1.7. I have used delegate() as well. In FF, Chrome it works as intended. In IE 10 it does not. I am at a loss. I knew that IE had issues with delegate in IE8 and with JQuery < 1.5 but this is not the case.
Does anyone know how to solve this?
I am answering the question just for future reference for other people. It seems that IE is caching AJAX requests for some reason I am unable to comprehend.
I noticed using the (surprisingly good) developer tools IE 10 provides that I was getting a 304 not modified response to my AJAX requests. This was not the case in Firefox or Chrome (200 was the response).
I added the cache: false option to my AXAJ JQuery functions and now it works as intended.
IE never seizes to amaze me.
Brief addition, given what (little) I understand on the subject. Apparently, the XmlHttpRequest spec says that XHR GET commands can behave just like a standard web page retrieval (e.g. clicking on a regular old link), and therefore XHR GET commands can be cached. The IE team has chosen to adhere to this spec, while the other browser makers have not. While I can see some logic in this approach, I think those of us who work with XHR requests every day would emphatically say that we would prefer caching to be off by default, rather than on. (-;
I ran into this a long long long time ago with IE... now I always make it a point now to write my ajax calls with a random trailing key/value pair.
I also add cache: false though I have found by itself it doesn't always do what it should (well, maybe its just IE not doing what it should)
This is how I set them up...
$('#trigger').submit( function(e){
e.preventDefault();
var randnum = Math.floor(Math.random()*1001); //magic starts here
$.ajax({
type: "POST",
url: "folder/file.php",
cache: false,
data: "random=" + randnum, //pure magic
success: function(){
// do stuff here
}
});
});
Got this issue too. It turns out that all of the fixes above will not work if the POST response has cache-control: max-age. The 1st request will fetch the data but after that all requests (even if you add a random attribute) will be 304'd.
In this case IE will not even ask the server if the content changed, it just assumes that if it has a max-age then there's no point in doing a request.
Moreover XHR specs say that 304's shouldn't pass any data so basically you get an empty response for a POST (just on IE 9 and 10).

Jquery Ajax multi user request

I am very new into ajax and i am having a strange problem, I have created an asp.net website that have a web service within the project I am calling this web service by following ajax code:
$.ajax({
type: "POST",
url: pageUrl + "/PassData",
data: JSON.stringify({ aos_code: code }),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnSuccessCall
});
the c# code is not very complicated, basically it will get the code and call the stored procedure and return some value:
[WebMethod(EnableSession = true)]
public string[] PassData(string aos_code)
{
// calling the database and returning some data
return data;
}
And it works fine; the problem is that if two different users accessing the application in the two different computers and let say they clicking a button at the same time to call the server side function via Ajax, it will return the “500 internal server error”.
I am not sure how exactly Ajax work, is request are belong to same session or all user requesting a single function?
Hope it makes sense, and I would really do appreciate for the clarification and help.
Regards,
Error 500 indicates that something went wrong on the server side. Check your console (firebug or similar) to see what exactly happened and try to fix it. Ajax has nothing to do with it, it is simply as if two users visited the same page at the same time.

Categories

Resources