How to get json in javascript or ajax - javascript

I have json data
how can i get this data using javascript
thanks :)
this is my code
var getJsonData = function(uri,callback){
$.ajax({
type: "GET",
dataType: "jsonp",
url: uri,
jsonpCallback: 'response',
contentType: "application/json",
success: function(json){
console.log(json);
callback(json);
}
});
}
var uri = pbxApi+"/confbridge_participants/conference_participants.json?cid="+circle;
getJsonData(uri, function(res){
}

var uri = pbxApi+"/confbridge_participants/conference_participants.json?cid="+circle;
getJsonData(uri, function(res){
var data = JSON.parse(res);
for(var objectIndex=0; objectIndex < data.length; objectIndex++){
var tempObject = data[objectIndex];
var id = tempObject.id;
var uid = tempObject.uid;
....
var updated_at = tempObject.updated_at;
}
}

you don't need use jsonp to get json data,
var getJsonData = function(uri,callback){
$.ajax({
type: "GET",
dataType: "json",
url: uri,
contentType: "application/json",
success: function(json){
console.log(json);
callback(json);
}
});
}
var uri=pbxApi+"/confbridge_participants/conference_participants.json?cid="+circle;
getJsonData(uri, function(res){
console.log(res
}

Related

How to write pretty URL based from data?

How to write pretty url from these data?
I need something like this https://mywebsite.com/admin/leads/count/data1/data2/data3
Thanks in advance. :)
var x = {data1, data2, data3};
$.ajax({
url: 'https://mywebsite.com/admin/leads/count/',
data: x,
type: 'GET',
contentType: "application/x-www-form-urlencoded",
datatype: "json",
async: false,
success: function(data){
$("#leads_count").val(data);
leadsCtr = data;
}
});
Take url as a string and manipulate it like this
var x = {data1, data2, data3};
var string = 'https://mywebsite.com/admin/leads/count/';
var fstring = string+x.data1+'/'+x.data2+'/'+x.data3;
$.ajax({
url: fstring,
data: x,
type: 'GET',
contentType: "application/x-www-form-urlencoded",
datatype: "json",
async: false,
success: function(data){
$("#leads_count").val(data);
leadsCtr = data;
}
});

get the return value of success in Iron-Ajax after POST

I'm currently working the project using Polymer and I'd like to get the return value of API after POST using Iron-Ajax.
Here is my sample code,
var rs = $.ajax({
type: "POST",
url: apiUrl,
data: _data,
dataType: "json",
contentType: 'application/json'
});
rs.done(function (data) {
console.log(data);
alert(data);
}
});
Ajax is asynchronous by default,you need add async:false to make it synchronous
var rs = $.ajax({
type: "POST",
url: apiUrl,
data: _data,
async:false,
dataType: "json",
contentType: 'application/json'
});
var result = null;
rs.done(function (data) {
console.log(data);
alert(data);
result = data;
}
});
//return result;//you can return value like this

How to access json response that have the following format

I get a response like this:
{
"data": [
"http:\/\/www.domain.com.br\/anunciantes\/jorgediaz.y.com.r\/26\/img1.jpg",
"http:\/\/www.domain.com.br\/anunciantes\/jorgediaz.t.com.r\/26\/img2.jpg"
]
}
I have tried:
$.ajax({
url: "/imovel/recuperar_fotos",
datatype: 'JSON',
contentType: 'JSON',
success: function (data) {
var i = 0;
while(i < 3)
{
alert(data[i]);
i++;
}
}
});
and also data[0][i] doesn't work.
It is because you are receiving an object that has a data property that is an array. Therefore you could iterate over response.data (or data.data, following your code naming).
Here you have 3 ways to iterate over an array (and avoid the while loop)
Using array's forEach method
$.ajax({
url: "/imovel/recuperar_fotos",
datatype: 'JSON',
contentType: 'JSON',
success: function (response) {
var photos = response.data;
photos.forEach(function(photo) {
console.log(photo);
})
}
});
Using the for ... in
$.ajax({
url: "/imovel/recuperar_fotos",
datatype: 'JSON',
contentType: 'JSON',
success: function (response) {
var photos = response.data;
for (var i in photos) {
console.log(photos[i]);
}
}
});
Using the classig for loop
$.ajax({
url: "/imovel/recuperar_fotos",
datatype: 'JSON',
contentType: 'JSON',
success: function (response) {
var photos = response.data;
for (var i = 0; i < photos.length; i++) {
console.log(photos[i]);
}
}
});
Try this
$.ajax({
url: "/imovel/recuperar_fotos",
contentType: 'application/json',
success: function(res) {
for (i = 0; i < res.data.length; i++) {
alert(res.data[i]);
}
}
});

Post array from javascript to php function using Ajax

i want to post an array from java script to php by ajax. But i don't know how do that, especially send it to php function like controller class. Correct me if i'm wrong, this is my java script source, as a function to send an array :
<script>
function send(){
var obj = JSON.stringify(array);
window.location.href = "post.php?q=" + obj;
}
</script>
i was try, but still fail. really need help..
As described in the JQuery API documentation, you can use
var rootPath="http://example.com/"
var jsonData = $.toJSON({ q: array });
var urlWS = rootPath + "post.php";
$.ajax({
url: urlWS,
type: "POST",
dataType: "json",
contentType: "application/json; charset=utf-8",
data: jsonData,
success: function(result) {
// do something here with returned result
}
});
var array= [];
array[0] = 'hi';
array[1] = 'hello';
$.ajax({
url: 'http://something.com/post.php',
data: {array: array},
type: 'POST'
});
try like this,
var data_to_send = $.serialize(array);
$.ajax({
type: "POST",
url: 'post.php',
data: data_to_send,
success: function(msg){
}
});
or
you can pass as json like below,
$.ajax({
type: "POST",
url: 'post.php',
dataType: "json",
data: {result:JSON.stringify(array)},
success: function(msg){
}
});
var arr = <?php echo json_encode($postdata); ?>;
ajax: {
url:"post.php"
type: "POST",
data: {dataarr: arr},
complete: function (jqXHR, textStatus) {
}
You can try this .this will work
example
ajax code:
$.ajax({
url: 'save.php',
data: {data: yourdata},
type: 'POST',
dataType: 'json', // you will get return json data
success:function(result){
// to do result from php file
}
});
PHP Code:
$data['something'] = "value";
echo json_encode($data);

i am getting XMLHttpRequest cannot load

i am getting XMLHttpRequest cannot load.so how to remove this error ,please any suggetion for that.
function soapRCA(){
var productServiceUrl = "https://www.ilportaledellautomobilista.it/eai/AreaVeicolo- ws/services/secure/coperturaRC";
var tipoDiVettura = $('#tipoVeicolo').val();
var targaperRCA = $('#targaRCA').val();
var SoaMessage = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">'+
'<soapenv:Header>'+
'<wsse:Security soapenv:mustUnderstand="0" soapenv:actor="http://schemas.xmlsoap.org/soap/actor/next" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">'+
'<wsse:UsernameToken wsu:Id="XWSSGID-1253605895203984534550" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">'+
'<wsse:Username>paolo.pelliccione</wsse:Username>'+
'<wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">tdlemon900mj</wsse:Password>'+
'</wsse:UsernameToken>'+
'</wsse:Security>'+
'</soapenv:Header>'+
'<soapenv:Body>'+
'<CoperturaRCVeicoloSecureRequest xmlns="http://www.dtt.it/xsd/Veicolo">'+
'<tipoVeicolo>'+tipoDiVettura+'</tipoVeicolo>'+
'<targa>'+targaperRCA+'</targa>'+
'</CoperturaRCVeicoloSecureRequest>'+
'</soapenv:Body>'+
'</soapenv:Envelope>';
$.ajax({
url: productServiceUrl,
type: "GET",
dataType: "xml",
data: SoaMessage,
complete: showResult2,
contentType: "text/xml; charset=\"utf-8\"",
crossDomain: true,
cache: false
});
return false;
}
function showResult2(xmlHttpRequest, status) {
//alert('edew00');
var data = $(xmlHttpRequest.responseXML).find('#listMovimenti tbody');
console.log(data);
//alert('ecco data: '+JSON.stringify(data));
/*var data = (''+response).substring((''+response).indexOf('<form id="ListaCoperturaRCVeicolo"'),(''+response).length);
data = data.substring(0,data.indexOf('</form>'));
$('#targapage #fintoiframe').html(data);
var ris = $('#targapage #fintoiframe').find('#ListaCoperturaRCVeicolo').html();
$('#targapage #risultatorcaEsterna').html(ris); */
}
</script>

Categories

Resources