$.ajax() to $.getJSON() - ajax

I would like to ask if there is a method to parse data from $.ajax() to $.getJSON() please?
This is the $.ajax() example:
var apiUrl = 'https://api.com/url_here';
var apiKey = 1234567890;
var apiSecret = 0987654321;
var data = {};
$.ajax({
url: apiUrl,
headers: {
"Authorization": "Basic " + btoa(apiKey+ ":" + apiSecret)
},
data: data,
dataType: 'json',
type: 'GET',
success: function(data) {
console.log(data);
},
error: function(data) {
console.log(data);
}
});
And this is what I have in my $.getJSON(), where I have a struggle in calling the headers for authentication:
$.getJSON(apiUrl, function(json){
console.log(JSON.stringify(json));
});
Thanks a lot! xxx

Related

Prevent Ajax form from being submitted twice?

Hello looking for some help with my form here.
How would I prevent " $(document).on('submit'..." getting triggered again when submitting it with " $(active_form)[0].submit();"?
Thanks!
$(document).on('submit', active_form, function(e) {
e.preventDefault();
active_form = $('div.account-address-form.visible form');
var vat_input = $('div.account-address-form.visible form input.vat_reg_no');
var but_prim = $('div.account-address-form.visible form .button-primary');
if (vat_input.val()) {
var url = "/apps/vat-check/";
var number = vat_input.val();
$.ajax({
type: "GET",
dataType: "jsonp",
url: url,
data: {
number: number
},
beforeSend: function() {
but_prim.prop('disabled', true);
but_prim.css('opacity', '.4');
},
success: function(data) {
var result = data.result;
if (result) {
$.ajax({
type: "GET",
dataType: "jsonp",
url: "/apps/customer-vat/",
data: {
email: "{{ customer.email }}",
vat_no: number,
update: true
},
success: function(data) {
$(active_form)[0].submit();
$(active_form).off("submit").submit();
$(document).on('submit', active_form, function(e) {
e.preventDefault();
if(!active_form.attr('validated'))
{
active_form = $('div.account-address-form.visible form');
var vat_input = $('div.account-address-form.visible form input.vat_reg_no');
var but_prim = $('div.account-address-form.visible form .button-primary');
if (vat_input.val()) {
var url = "/apps/vat-check/";
var number = vat_input.val();
$.ajax({
type: "GET",
dataType: "jsonp",
url: url,
data: {
number: number
},
beforeSend: function() {
// disabling button
but_prim.prop('disabled', true);
// set button
but_prim.css('opacity', '.4');
},
success: function(data) {
active_form.attr('validated',true);
var result = data.result;
if (result) {
$.ajax({
type: "GET",
dataType: "jsonp",
url: "/apps/customer-vat/",
data: {
email: "{{ customer.email }}",
vat_no: number,
update: true
},
success: function(data) {
active_form.attr('validated',true);
$(active_form)[0].submit();

Display all data fetched, api rest using __next

I need to fetch all data from an xml link but I couldn't as it displays only 300 rows, so I found a solution that says I should use __next, I have the code below but it doesn't work, in the console I get the next url but the items (TaskName) of the first page. I want to get the TaskName(s) of the next pages.
window.addEventListener('load',function() {
$.ajax({url: _spPageContextInfo.siteAbsoluteUrl + "/_api/ProjectData/[en-US]/Tasks",
method: "GET",
dataType: "json",
headers: {Accept: "application/json;odata=verbose"},
success: function(data) {
var dataResults = data.d.results;
if (data.d.__next) {
url = data.d.__next;
console.log("url: "+url);
}
$.each(dataResults, function(key, value)
{
var tasky = value.TaskName;
console.log(tasky);
});
}});
});
I found a solution, here the code is:
function GetListItems(){
$.ajax({
url: urly,
method: "GET",
headers: {
"Accept": "application/json; odata=verbose"
},
success: function(data){
response = response.concat(data.d.results);
if (data.d.__next) {
urly = data.d.__next;
GetListItems();
}
$.each(response, function(key, value)
{
var tasky = value.TaskName;
console.log(tasky);
});
},
error: function(error){
}
});
}

display values returned by json

I have simple question to display data on html page. Following code displays array of json data on screen. but, I want to display it by each element such as "url", "img_url" and so on.
could you please let me know who to do it ?
ajax code
var dataString = 'url=' + pathname + '&img_name=' + img_name + "&tag=" + tag;
$.ajax({
type: "POST",
url: "image_finder.php",
data: dataString,
dataType: 'json',
complete: function (xhr, status) {
if (status === 'error' || !xhr.responseText) {
//handleError();
alert("error");
} else {
var data = xhr.responseText;
$('#tt').html("<div id='message'></div>");
$('#message').html(data);
}
}
});
json return
{"cid":"14","url":"http:\/\/localhost\/","img_url":"http:\/\/static.naver.net\/www\/up\/2013\/0305\/mat_173330634c.jpg","img_name":"mat_173317134c.jpg","html":"<div id=\"hotspot-19\" class=\"hs-wrap hs-loading\">\r\n<img src=\"http:\/\/static.naver.net\/www\/up\/2013\/0305\/mat_173330634c.jpg\">\r\n<div class=\"hs-spot-object\" data-type=\"spot\" data-x=\"95\" data-y=\"64\" data-width=\"30\" data-height=\"30\" data-popup-position=\"left\" data-visible=\"visible\" data-tooltip-width=\"200\" data-tooltip-auto-width=\"true\">\r\nasdf\r\n<\/div>\r\n<div class=\"hs-spot-object\" data-type=\"spot\" data-x=\"168\" data-y=\"53\" data-width=\"30\" data-height=\"30\" data-popup-position=\"left\" data-visible=\"visible\" data-tooltip-width=\"200\" data-tooltip-auto-width=\"true\">\r\nrere\r\n<\/div>\r\n<\/div>\r\n","jscript":""}
$.ajax({
type: "POST",
url: "image_finder.php",
data: dataString,
dataType: 'json',
success: function (data) {
for(var item in data){
console.info(item);//print key
console.info(data[item]);//print value
}
}
});
I hope this is what you need.

using success event ofjquery / ajax to fill and set dropdownlist value

i have a Ajax request to Fill an edit form like..
function FillLiveDetail(event, LiveID)
{
$.ajax
({
type: "POST",
url: MyUrl + '/GetLiveDetail',
data: '{LiveID: "' + LiveID + '"}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(data)
{
var LiveDetail = data.d;
$('#ClassName').val(LiveDetail[0].Title);
$('#ClassDesc').val(LiveDetail[0].Desc);
$('#SearchKey').val(LiveDetail[0].Keys);
$('#Date').val(LiveDetail[0].Date);
$('#Hour').val(LiveDetail[0].Hours);
$('#Minute').val(LiveDetail[0].Minuts);
$('#AM_PM').val(LiveDetail[0].AmPm);
$('#Duration').val(LiveDetail[0].Duration);
$('#rdbLivePrivacy).prop('checked',true);
CategoryList(event, 'MyddlCategory');
$('#MyddlCategory').val(LiveDetail[0].CategoryID);
}
});
}
function CategoryList(event, ddl_Category_ID)
{
var ddl_Category = $('#'+ddl_Category_ID);
event = event || window.event || e.srcElement;
event.preventDefault();
$.ajax
({
type: "POST",
url: ServiceUrl +'/FillAllCategories',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(data)
{
var MyCategoryList = data.d;
$(ddl_Category).html('<option value="0" selected="selected">All Category</option>');
for(var m=0; m<MyCategoryList.length; m++)
{
var row = ['<option value="'+MyCategoryList[m].CategoryID+'">'+MyCategoryList[m].CategoryName+'</option>'].join('\n');
$(ddl_Category).append(row);
}
}
});
}
My problem is, I can not change the definition of my function CategoryList() because i used it at many places in my project...
In this context the success: event of CategoryList() function Fire when success: event of FillLiveDetail() function completes that is why i am unable to set the value of MyddlCategory...
please suggest me any thing that may work..
Close the selector quote in this line $('#rdbLivePrivacy').prop('checked',true);
Try setting async property of ajax call to false
$.ajax
({
type: "POST",
async: false,
url: ServiceUrl +'/FillAllCategories',
...

how to get the json data from the url in jquery?

How to get the json array from the URL.I declare the mydata as a variable.how to get the json array into the mydata? using the following ajax
var mydata;
$.ajax({
url: someurl,
dataType: 'json',
success: function() {
}
});
var mydata;
$.ajax({
url: someurl,
dataType: 'json',
success: function(data) {
mydata = data;
console.log(mydata);
}
});
alternatively you could use $.getJSON() function
Try this:-
***$.ajax({
url: someurl,
dataType: 'json',
success: function(mydata) {
foreach(var data in mydata){
//do something.....
}
},
failure: function()
{
//error message
}
});***

Resources