Is it possible to send large amount of data (grid content for example) in $.ajax, to controller?
Are there workarounds of "URI too long" thing?
I know it's probably not the best practice, instead I should probably send each row one by one, but still is it possible?
Are there workarounds of "URI too long" thing?
Use a POST HTTP verb instead of GET:
$.ajax({
url: '/foo',
type: 'POST',
data: { value: someVariableThatCouldBeHuge },
success: function(result) {
// TODO: process the results
}
});
or the equivalent:
$.post('/foo', { value: someVariableThatCouldBeHuge }, function(result) {
// TODO: process the results
});
Related
So I have a stupid question about this:
$.ajax({
type: "GET",
url: `URL`,
data: DO STUFF WITH WHAT I GOT FROM THE REQUEST???,
});
In ajax, when I make a get request from a URL, with the data: parameter am I giving a response that is data or is data the data I received from the request?
You can do something with the data in the success part of the ajax call:
$.ajax({
dataType: 'json',
url: url,
data: data,
success: success
});
In this case, a potential success callback would look like this:
function success(data) {
// do something with data, which is an object
}
or if there is no data to send:
function testAjax(handleData) {
$.ajax({
url:"getvalue.php",
success:function(data) {
handleData(data);
}
});
}
The main thing to understand here is that any AJAX call (any web request really) has two components: A request and a response. The actual $.ajax() function call is sending the request, and a callback function is provided to handle the response.
To illustrate:
$.ajax({
type: "GET", // request type
url: "http://www.example.com/someResource", // destination URL
data: { name: "David", location: "Boston" } // data to send
});
This would make a GET request to the specified URL, sending it the specified data. The response in this case is ignored, since no callback is provided. But you can provide one:
$.ajax({
type: "GET",
url: "http://www.example.com/someResource",
data: { name: "David", location: "Boston" }
}).done(function(response) {
// handle the response
});
The function which contains "handle the response" will be called by the system when the AJAX response is received from the server. The response variable (or whatever you want to call that variable, the name doesn't matter) will contain whatever the server sent in return. Which could be anything, really.
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 have a very basic ajax call to alert the data that was reported from the server
$.ajax({
type: "POST",
url: "/someform/act", //edit utl to url
data: { changed: JSON.stringify(plainData) }, //edit to include
success: function(data) {
alert(data); //data not $data
},
error: function() {
//error condition code
}
});
According to the docs on the jquery website regarding data field on the success callback, it says that data returned is the data from the server. However for some strange reason when I alert $data, I get [object Object]
I was expecting to see something like this, since that is what the server would send back
<status>0</status>
EDIT:
data is also passed along as the POST
You need to use JSON.stringify(data) in the alert to get anything readable.
Also, $data is a completely different variable name than data.
alert() prints the string representation of the arguments - hence if you pass an object, you'll get [object Object].
To inspect data, use console.log(data) better.
If you server send a JSON, you need to put dataType: 'json' to your ajax call. Be aware there's some mistake in your ajax call.
$.ajax({
type: "POST",
url: "/someform/act", // NOT 'UTL',
data: {
key: value,
key2: value2
},
// or data: plaindata, // If 'plaindata' is an object.
dataType: 'json',
success: function(data) {
console.log(data); // As moonwave99 said
},
error: function() {
//error condition code
}
});
EDIT
When sending data, you should send an object. jQuery will handle the array to sned it to the server. So if plain data is an object, it should be like this
data: plainData,
If you're sending data via $.ajax({...}), the Network tab of your browser inspector might be showing [object Object] in the Payload (Chrome) / Request (Firefox) sub-tab, like in the following image (Firefox):
The reason for this might be in the way you're forming your AJAX call. Specifically:
$.ajax({
url: '/ajax/example-endpoint',
data: {'fooKey':fooData,'barKey':barData},
type: 'post',
cache: false,
contentType: false, // this one will turn your data into something like fooKey=fooData&barKey=barData
processData: false, // and this one will make it [object Object]:""
beforeSend: function() {
// whatever it is you need to do
},
success: function(data) {
// do stuff
},
error: function(desc, err) {
// do stuff
}
});
When combined, contentType: false and processData: false turn your data into [object Object], because you're actually telling your AJAX call to ignore the content type of whatever is being sent, and not to process it.
If it's IIS, try creating a site outside of the Default Web Site (for example localhost/ajax1). For example a new site ajax1, place it not in the DefaultAppPool, but in your pool, for example ajax1. Try http://ajax1
I switched to POST searches using ajax in my application so I can start using the date range. However it seems no matter what I post it keeps returning the first 10 results in my index. The true results are in the 30k range.
amplify.request.define("searchPostRequest", "ajax", {
url: "http://leServer:9200/people/person/_search",
type: "POST",
dataType: 'jsonp',
contentType: 'application/json'
});
self.advancedSearchPostQuery = {
query: {
term: {
locationNumber:479
}
}
};
console.log(self.advancedSearchPostQuery);
amplify.request({
resourceId: "searchPostRequest",
data: JSON.stringify(self.advancedSearchPostQuery),
success: function (data) {
console.log(data.hits.hits.length);
}
});
If this is your actual code, your problem might simply be that your
advancedSearchPostQuery isn't valid JSON.
You need to apply quotes:
advancedSearchPostQuery = {
"query": {
"term": {
"locationNumber": 479
}
}
}
And I'm not sure if you need to stringify the object, but I'm not familiar with amplifyJS, so double check on that as well if amplifyjs is expecting an object or a string.
If that doesn't help check if your query returns the correct results when running from command line through curl.
After doing more debugging I found that the request was being sent as a GET even though I had explicitly set it to post. Moving the data type to json, from jsonp let the request be sent as a POST, which resolved the issue. However this causes an issue in IE where the request is not sent at all due to the request being sent to another domain.
amplify.request.define("searchPostRequest", "ajax", {
url: "http://leServer:9200/people/person/_search",
type: "POST",
dataType: 'json',
contentType: 'application/json'
});
Don't know if it matters, but the msg.d is about 300 rows long. I get properly formatted Json data when I alert msg.d.
$("#supplierSelect").autocomplete({
source: function( request, response ) {
$.ajax({
type: "POST",
url: "SupplierAdmin.aspx/PopulateSupplierSelectDropDownList",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
response( $.map( msg.d, function( item ) {
return {
label: item.title,
value: item.turninId
}
}));
}
});
}
}).fadeIn();
Lots of thanks if you can tell me how to only fadeIn when success.
Edit: The "properly formatted Json data" actually has quotes around the label and value, and the ordering is switched. Checking now to see if it makes a difference. Sorry for incomplete info.
Edit2: I went with response( $($.parseJSON(msg.d)).map( function()... instead, and now when I type anything, the autocomplete drops down the whole list of names and selecting one puts the value in the box. Am I right to assume this isn't correct functionality?
Needed to add parseJSON to the map.