I have a C# web application which uses ajax method to GET and POST data. Is there any difference between GET and POST methods in passing data (in case of contentType,data,dataType)?
$.ajax({
type: 'GET',
url: "url",
contentType: "application/json; charset=utf-8",
data: { value: "data" },
dataType:"json",
success: function (data) {
alert(data);
},
error: function (data) {
alert("In error");
}
});
});
GET encodes the information into the url, the more info you GET the longer your URL becomes.
POST stores data in an array and passes that array to the next page. your Url remains unmodified.
While that may not seem like a huge deal, URLs do have a maximum length and errors will ensue if you exceed it. In addition and call to a specific url may fail due to the modifications GET makes. Apart from that, They are similar enough in function to be interchangeable for most purposes.
In normal form method also GET is used to sent some insensitive small chunk of data to server in querystring, whereas POST is used for sending large and secure data to the server
In case of using ajax GET is commonly used, POST is feasible only when you have to do DB interactions on server or there's some sensitive data involved, read more here http://www.jquery4u.com/ajax/key-differences-post/
Related
I was asked in an interview what are the methods in AJAX other than GET and POST. I googled but I couldn't find any . Could someone please tell me.
HEAD Same as GET but returns only HTTP headers and no document body
PUT Uploads a representation of the specified URI
DELETE Deletes the specified resource
OPTIONS Returns the HTTP methods that the server supports
These are generally used methods
Also see detail about
There are four types of calls you cand do to a REST API GET,POST,PUT and DELETE. PUT should be suposedly used to modify already existing data while DELETE should be used to eliminate data. Still due to convention, PUT and DELETE are barely used. Here are a couple of examples of PUT and DELETE Ajax calls
$.ajax({
url: urlCalltoDeleteResource),
type: 'DELETE',
success: function(data) {
alert('Deleted');
},
error: function(data) {
alert('Not Deleted');
},
});
$.ajax({
url: urlToUpdate,
type: 'PUT',
data: "name=NameToUpdate",
success: function(data) {
alert('Load was performed.');
}
});
This is how I make my post request. data contains an array of selectedIds and it's possible with the traditional property. The problem is that if I need to POST 200 selectedIds the URL is to long and it breaks. What is best pratice to solve this situation? Only idea I can think of is looping my ids and POST'ing them in smaller chunks, but don't know the downsides or there is a more standard way of doing this?
POST:
var addToBuffer = function(url, data) {
return $.ajax({
traditional: true,
url: url,
dataType: "html",
data: data
});
},
Url ( there is 200+ selectedIds ):
http://localhost/foo/Buffer/AddToComputerAndDevicesBuffer?selectedIds=2639&selectedIds=5386&selectedIds=3225&selectedIds=6791&selectedIds=3231&selectedIds=357 ...
Error message:
"HTTP Error 404.15 - Not Found"
"The request filtering module is configured to deny a request where the query string is too long."
"Most likely causes:"
- "Request filtering is configured on the Web server to deny the request because the query string is too long."
"Things you can try:"
- "Verify the configuration/system.webServer/security/requestFiltering/requestLimits#maxQueryString setting in the applicationhost.config or web.config file."
Update 1:
Just realized just before posting this question, that I am not POST'ing but using GET, and it works with 200 ids, when I used:
var addToBuffer = function(url, data) {
return $.ajax({
type: "POST",
traditional: true,
url: url,
dataType: "html",
data: data
});
},
But that quickly raises another question, will this still works when I scale up to 10k+ or even 1M+? Do I get a timeout or am I DDos'ing my own server? Am I back to the loop with smaller chunks solution, or does anyone have any good advises how they solved this before? Thanks.
I'm using web api without deep understanding what it is, just knowing that each editable entity become a resource, that means has the uri, when web api provides the interpretation of PUT, POST, GET, DELETE HTTP commands to support CRUD operations. But what if for tracing/logging purpose I need to send correlation token together with e.g. GET request? Are there any recommendations and techniques to add to the HTTP request/routing "technical parameters"?
I have found something that need to be tested https://webapicorrelator.codeplex.com/ But actually I would prefer just to understand how it could work...
Or just add it to the heder using jquery ajax headers:
return $.ajax({
// have to use synchronous here, else the function
// will return before the data is fetched
url: url,
data: { ElectrodeId: electrodeId },
headers: { "X-CorrelationToken": correlationToken },
method: "POST",
dataType: "json",
success: function (data) {
}
We have an ios application built with trigger.io. this application is using forge.request.ajax to send data to our servers. one of our requests occasionally throws an error and returns this:
{"message":"Invalid parameter not satisfying: url","type":"UNEXPECTED_FAILURE"}
since the input parameters are sent in json format I suspected that some characters inputted by users, could break the structure and cause this error. my code looks like this:
forge.request.ajax({
url: "someurl.php",
dataType: "json",
data:"some=data&and=some&more=data&which=is inputted by user",
success: function (data) {
},
error: function (error) {
forge.request.ajax({
url: "errorlog.php",
dataType: "json",
data:"data=" + encodeURIComponent(JSON.stringify(error)),
success: function (data) {
},
error: function (error) {
}
});
}
});
this code gives the above error half the time. and work on the other half. are there any limitations for input parameters in ajax request? since i can't edit objective-c code, i need a solution - preferably a filter- which ensures this function to work with whatever input is entered.
Using encodeURIComponent may help:
var data = "some=data&and=some&more=data&which=is inputted by user";
forge.request.ajax({
url: "someurl.php",
dataType: "json",
data: encodeURIComponent(data)
...
Passing request data as URL Parameters has more than it's share of gotchas though so it may also be worth taking a look at this StackOverflow question: When are you supposed to use escape instead of encodeURI / encodeURIComponent?
I am loading a large (~300 MB) JSON file by using the following code:
$.ajax({
type: 'GET',
url: path,
dataType: 'json',
data: {},
async: false,
success: function(json_object) {
console.log("success!");
} error: function(request, error) {
console.log(request["statusText"]);
}
});
Running it outputs "InternalError: allocation size overflow". Is there any way to get around this that does not involve making the file smaller?
You'll need to set up a buffer. However, why on earth are you passing so much data? That would be an extremely unreasonable wait for any user.
EDIT
Buffering isn't really something you can do from the ajax side (according to How to buffering an Ajax Request?). However, you can set something up server side (if it's your server returning the data) to send it in pieces, then use ajax to request each piece.
If it's not your server, or your requesting from an API or something, then look and see if they accept any parameters to define the size of the return object - this way you can request it in chunks.