Hi I am trying to make a simple call like this in ajax:
$.ajax({
type: "GET",
url :"${info.contextPath}/secured/contribution/employer/contributionsearch/viewNewContribution?employerCode="+code+"&contbYear="+year+"&contbMonth="+month
+"&orgId="+orgId+"&contbStatus="+status+"&employerDetailId="+employerId,
success: function(data){
alert('123'+data);
$("#newContribution").html(data);
}
});
However, when I see the pop up of my alert I only see '123'. Is my call wrong ?
Use data: {key: value} instead of url concatenation.
Make sure that server's response is correct
Check for the server's response. If possible follow this approach :
$.ajax( {
method : 'GET',
url : '/uri',
data : {key1 : "value1", key2 : "value2"}
}).success(function (data) {
console.log(data);
alert(data);
});
Just try to serialize the response in alert.
$.ajax({
type: "GET",
dataType: "json",
url :"${info.contextPath}/secured/contribution/employer/contributionsearch/viewNewContribution?employerCode="+code+"&contbYear="+year+"&contbMonth="+month +"&orgId="+orgId+"&contbStatus="+status+"&employerDetailId="+employerId,
success: function(data){
var resp = JSON.stringify(data);
alert('123'+resp);
$("#newContribution").html(resp);
}
});
Related
Im returning specific data in to my jquery using C, how will i put the data in C?
str
function Run() {
$.ajaxSetup({ cache: false });
var obj = {"method":"pref-get","arguments":{"infos":["sys_info"]}};
alert("Post Json:" + JSON.stringify(obj));
$.ajax({
url: "http://localhost/cgi-bin/try.cgi",
type: 'POST',
data: JSON.stringify(obj),
dataType: 'text',
success: function(response){
alert(response);
}
});
}
Run();
the Value_in_C must have the data in the CGI.
Heres the code for C:
if(!end && !strcmp(method, "GET"))
printf(("%s"), str);
response = (("%s"), str);
fclose(fd);
How can I pass or return the value of str to the alert function?
Or should i add function in C to response in POST/GET method?
Thanks in advance!
Your question is an basic ajax problem, isn't it?
You have: client > call "try.cgi" in server > response data ("Value_in_C") to client > display it.
Then it should be:
$.ajax({
url: "http://localhost/cgi-bin/try.cgi",
type: 'POST',
data: JSON.stringify(obj),
dataType: 'text', // since your response is a string
success: function(responseData){
alert(responseData); // responseData should be "Value_in_C"
}
});
Updated: dataType: 'json' will parse response to json, so "Value_in_C" became null
I'm new to Jquery and Ajax calls... This is my call:
$(document).ready(function () {
$.ajax({
type: "GET",
url: "some url",
success: function(response){
console.log(response);
}
})
});
The console log show this response:
{"2014/08/08":[{},{"TEST":0}]}
How can I save the value of TEST to a variable?
Similar to var t = document.getElementById('test').value
using JSON.parse
$(document).ready(function () {
$.ajax({
type: "GET",
url: "some url",
success: function(response){
try{
json = JSON.parse(response);
test = null;
$.each(json,function(i,item){
test = item[1].TEST;
});
alert(test);//this is what you want
}catch(e){}
}
})
});
The response from the server is json, so the best way to handle it is to tell jQuery to expect a json answer and turn it into a regular javascript object. This is done by adding the dataType: 'json' to your $.ajax(...) call :
$.ajax({
type: "GET",
url: "some url",
dataType: "json",
success: function(response){
console.log(response);
}
})
You can alternatively use the shortcut $.getJSON(...) :
$.getJSON("some url", function(response){ console.log(response); });
Now that you have a regular javascript object, you can use what other answers suggest :
success: function(response) {
console.log(response["2014/08/08"][1].TEST);
// or
$.each(response, function(i, itm) {
console.log(itm[1].TEST);
};
}
"want to save the value of TEST to a variable" Try this:
var t = response["2014/08/08"][1].TEST;
Demo
try this,
$.each(response,function(i,value){
alert(value[1].Test);
});
If I need to call a controller like this:
name.php?data={"user":"test","pass":"test"}
In order to obtain the information I need, via .ajax, I need help setting the variable to be sent with that specific format.
I used to the following code:
var arr = [{
data: {
"user" : $("#usuario").val(),
"pass" : $("#password").val()
}];
arr = JSON.stringify(arr);
However if doesn't send the right output, I've been said I need to send the variable with the json on it.
function callAjax(url, arr) {
var response = null;
jQuery.ajax({
url: url,
type: 'POST',
data: arr,
contentType: 'application/json; charset=utf-8',
dataType: 'json',
async: false,
success: function(data) {
response = data;
},
error: function(jqXHR, textStatus, errorThrown) {
response = errorThrown;
},
timeout: 5000
});
return response;
}
Any advises?
Best Regards!
This link will help you a lot!
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify
I used .post instead and solved the issue
Here is my script:
$.ajax({
type: "Get",
url: "Sample.js",
datatype: 'json',
data: JSON.stringify({ key:key }),
success: function (data) {
var sample = data.name;
$("#html").html(sample);
},
error: function () {
alert("Error");
}
});
This is my Sample.js file:
{ "name": "user" }
When I run this code I get a blank screen. This is my script using getJSON():
$.getJSON("Sample.js", function (data) {
var sample = data.name;
$("#html").html(sample);
})
This produces "user" perfectly. What is the problem with $.ajax code?
In the getJSON version your don't send any data. Could this be the reason why that works? To me it looks like this could be sth. on the server side that delivers an empty JSON object when you pass the key parameter.
As the jQuery documentation states:
$.ajax({
dataType: "json",
url: url,
data: data,
success: success
});
Try modifying the dataType param.
change your datatype to dataType. Its case sensitive. Refer http://api.jquery.com/jQuery.getJSON/
Remove JSON.Stringify and change Get to GET
$.ajax(
{ type: "GET",
url: "Sample.js",
dataType: "json",
data: {key:key },
success: function (data)
{ var sample = data.name; $("#html").html(sample); },
error: function () { alert("Error"); }}
);
I have a problem here and no idea how to solve it...
I have a json file like this:
{"data":[{"kw":"48","val":"10","val2":"05"},{"kw":"49","val":"04","val2":"05"}]}
But I need this format:
[{"kw":"48","val":"10","val2":"05"},{"kw":"49","val":"04","val2":"05"}]
In javascript/jQuery I make an ajax request and get the json back:
$.ajax({
type : "POST",
cache : "false", // DEBUG
url : weburl,
dataType : "json",
contentType : "application/json; charset=utf-8",
success : function(data) {
// Strip data?
}
});
Does anyone know how to do this?
Thanks!
success : function (data) {
var array = data ? data.data : null;
// now perform the required operations with array variable.
}
This will return just the array, not wrapped in a object.
$.ajax({
type: "POST",
cache: "false", // DEBUG
url: weburl,
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function(data) {
var arrayYouWant = data.data; // http://thedailywtf.com/Articles/Data-Data-data-Data.aspx
}
});
Why do you need to strip it, you just reference it
success : function(data) {
var myArrayofObjects = data.data;
}
In order to really understand read about the Member operators in Javascript, particularly the dot notation. JSON is a subset of Javascript and the JSON object is a Javascript object in the end.
Not sure what you mean by archive. Do you mean simply access the array that is associated with the data property?
The array is associated to the 'data' property in your JSON string. I would maybe change the name of the data argument passed into the success function.
Try this:
$.ajax({
type: "POST",
cache: "false", // DEBUG
url: weburl,
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function(resp) {
var yourArray = resp.data;
console.log(yourArray);
}
});