how we can send the Javascript array as a parameter in the ajax get call.
I am working with the API call and i have the API call method that has three parameters and the third one is the array.
You can do like this
demArray = [];
demArray[0] = 'hi';
demArray[1] = 'hello';
$.ajax({
type: "GET",
data: {arrayDem:demArray},
url: "someUrl",
success: function(response){
}
});
Also serialize() & serializeArray() can help you.
If you are getting those arrays from a form you could:
var form = $('#form').serialize();
$.ajax({
type: "GET",
data: form,
url: "foo.php",
success: function(data){
}
});
Else you could just pass the parameters into data: like:
data: {foo:Array}
Related
$("#nonjstest").click(function() {
$.ajax({
url:"https://petstore.swagger.io/v2/pet/findByStatus?status=sold",
type: "GET",
contentType:"application/json",
success: function(data){
alert(data[0].id)
}
})
})
Here, I am passing the required parameters directly in the URL status=sold.
I want to pass them via a variable instead, but it's not working. I've tried the following:
var requiredata = ["status=sold"]
$("#nonjstest").click(function() {
$.ajax({
url:"https://petstore.swagger.io/v2/pet/findByStatus",
type: "GET",
data: requiredata,
contentType:"application/json",
success: function(data){
alert(data[0].id)
}
})
})
The request itself is successful, but nothing is returned, meaning, the parameters are not passed correctly. What is the problem with the way I'm passing the Array string?
I've tried stringifying the data, but it didn't work either.
Why is it not working? How do I pass the array string data with a variable?
Try sending the params as an object instead of as an array:
var requiredata = {
status: 'sold'
};
$("#nonjstest").click(function() {
$.ajax({
url:"https://petstore.swagger.io/v2/pet/findByStatus",
type: "GET",
data: requiredata,
contentType:"application/json",
success: function(data){
alert(data[0].id)
}
})
});
Another option would be to pass the params as a string but it's worse because you have to pass it already encoded.
Check the docs for more information.
This question already has answers here:
jQuery: serialize() form and other parameters
(12 answers)
Closed 5 years ago.
I have Ajax like this.
var idlapkondisi = $('#id_laporan_pemeriksa').val();
var data = $('#myFormkondisi').serialize();
$.ajax({
type:'ajax',
method:'POST',
url:url,
dataType:'json',
success:function(response){
},
error:function(response){
console.log(response);
}
})
})
how can i make 2 value in ajax data while the value is a serialize data form ?
does the format like this ?
data:{data,idlapkondisi:idlapkondisi},
Use serializeArray instead of serializing.
var idlapkondisi = $('#id_laporan_pemeriksa').val();
var data = $('#myFormkondisi').serializeArray();
data.push({name: "idlapkondisi", value: idlapkondisi});
$.ajax({
type: 'ajax',
method: 'POST',
url: url,
data: data,
dataType: 'json',
success: function(response) {
},
error: function(response) {
console.log(response);
}
});
Try this to add that value to you payload:
var idlapkondisi = $('#id_laporan_pemeriksa').val();
var data = $('#myFormkondisi').serialize();
data["idlapkondisi"] = idlapkondisi;
Then you can add data to your Ajax call as usual.
I would like to send a GET request with only one parameter from an input form via AJAX, using jQuery.ajax().
To simplify things, instead of
data: $("#the_form").serialize()
I tried to explicitly pass the value of the input:
function lookupSearch(val){
$.ajax({
type: "get",
url: "/search",
data: {
tx_search_pi1['sword']: val
},
success: function(data)
{
$("#results").html(data);
console.log("Success");
}
});
}
Now this breaks because of the brackets and quotes in
tx_search_pi1[sword]: val
(which is required by the recipient of the get request)
How do I escape the brackets (and maybe also single quotes inside= correctly?
I've tried-and-errored many combinations, eg. with
tx_search_pi1%5Bsword%5D
encodeURIComponent("tx_kesearch_pi1[sword]")
etc...
You can try this,
data: $("#the_form").serialize()+'&sword='val,
Full Code,
function lookupSearch(val){
$.ajax({
type: "get",
url: "/search",
data: $("#the_form").serialize()+'&sword='val,
success: function(data)
{
$("#results").html(data);
console.log("Success");
}
});
}
And if you want to pass sword value then use,
data: {'sword':val}, // get using sword key
As per #Urs Comment the code should be,
// let it is initialised before,
// and it is an object like {}, tx_search_pi1 = {key:"1",...};
function lookupSearch(val){
tx_search_pi1['sword'] = val;
$.ajax({
type: "get",
url: "/search",
data: tx_search_pi1,
success: function(data)
{
$("#results").html(data);
console.log("Success");
}
});
}
try putting tx_search_pi1['sword'] this in a variable and pass it.
Like
var temp = tx_search_pi1['sword'];
And pass temp
I have My jquery function that is returning me data of the formatt:
{"Suggestions":[{"name":"RR Restaurant","category_id":"166","locality":"Gayathri Nagar","listing_info":"listing","random_id":"1ll0f0wJ"},{"name":"Oko Restaurant","category_id":"166","locality":"Kumara Krupa","listing_info":"listing","random_id":"0F7ZGV9p"},{"name":"H2O Restaurant","category_id":"166","locality":"Maratha Halli","listing_info":"listing","random_id":"0JNPOyuP"},{"name":"Taj Restaurant","category_id":"166","locality":"Shivaji Nagar","listing_info":"listing","random_id":"7GeA0Kfq"},{"name":"PSR Restaurant","category_id":"166","locality":"Peenya Industrial Area","listing_info":"listing","random_id":"cRvJCwQ3"},{"name":"ac restaurant","category_id":"166","listing_info":"keyword"},{"name":"Indian Restaurant","category_id":"166","listing_info":"keyword"},{"name":"goan restaurant","category_id":"166","listing_info":"keyword"},{"name":"thai restaurant","category_id":"166","listing_info":"keyword"},{"name":"andhra restaurant","category_id":"166","listing_info":"keyword"}],"ImpressionID":"test"}
I wish to parse the same to get multiple variables with The field "Name" and "Random Id" in different js variables
$("#what").on("keypress", function() {
$.ajax({
type: "GET",
cache: false,
url: "/AutoComplete.do",
data: {
query: 'Pest',
city: 'Bangalore'
}, // multiple data sent using ajax
success: function(data) {
alert();
}
});
});
My JSON object seems to be nested with "Suggestions" as the parent. Please help.
If you add a property to $.ajax function you be ensure that is json parsing:
dataType: 'json'
EDIT
To iterate above the string you can use for(in) or each() jquery
json = "[{'key':'value'},{'key':'value']";
for(var i in json) {
console.log(json[i]);
//if you see in console [OBJECT Object] you are
//in a new object you must to iterate nested of this.
}
$("#what").on("keypress", function() {
$.ajax({
type: "GET",
dataType: "JSON", //You need this to be inserted in your ajax call.
cache: false,
url: "/AutoComplete.do",
data: {
query: 'Pest',
city: 'Bangalore'
}, // multiple data sent using ajax
success: function(data) {
alert();
}
});
});
After insert dataType you can probably do this.
console.log(data.Suggestions);
Also take a look at the API Doc at below url regardless of dataType.
http://api.jquery.com/jquery.ajax/
The data object you are specifying is what will be sent as the arguments to your success method, if you use the GET method.
$("#what).on("keypress", function() {
$.get("/AutoComplete.do", function(response) {
var data = JSON.parse(response);
//data.suggestions = [lots of objects];
//data.suggestions[0].locality = "Gayathri Nagar"
});
});
I'm making ajax POST request from javascript function:
function UpdateMetrics() {
$.ajax({
type: "POST",
url: "MyHandler.ashx?Param1=value1",
data: "{}",
contentType: "text/json; charset=utf-8",
dataType: "text",
success: function (msg) {
var jsonUpdatedData = msg;
...
}
});
}
From my handler, I'm sending json string with:
context.Response.write(json);
I think I'll get it in msg.
I also want to send other string (count). So I'm trying to use header info along with json data. So I added this line:
context.Response.Headers.Add("MaxCount",Convert.ToString(tempList.Count));
If this is right way to do it, how can I access it in my success function?
To access headers in your success function, add in 2 more arguments to your function, the status code and the jqXHR object, which you can read the documentation for at api.jquery.com.
So, your function should look like:
success: function (msg, status, jqXHR) {
var jsonUpdatedData = msg;
...
}
However, as pointed out in comments, it's probably best not to use the header to send data. You should probably just include it in the json you send out.
You also need to tell jQuery to interpret the response as json by setting
dataType: "json"
Otherwise, it will just be returned to you as text.
Your requirement to get the header data in ajax post success can be achieved using getResponseHeader method please refer the below code snippet.
function UpdateMetrics() {
var callback = $.ajax({
type: "POST",
url: "MyHandler.ashx?Param1=value1",
data: "{}",
contentType: "text/json; charset=utf-8",
dataType: "text",
success: function (msg) {
var jsonUpdatedData = msg;
var headerdata = callback.getResponseHeader("MaxCount");
// Where MaxCount is name provided in the header.
...
}
});
}
Thanks