קבלת מידע מ-API
-
@clickone אמר בקבלת מידע מ-API:
$.get("https://shutaf.im/php/phone.php?oper=getTotalDonations&cmp_id=640", function(data, status){ alert("Data: " + data.data.donation_sum_amount + "\nStatus: " + status); });
@dovid צד לקוח
@clickone נראה כיון טוב, אבל לא תפסתי איך אני מגיע לפרמטר שמכיל את המידע להשתמש איתו
גם alert לא עובד אצלי, כלומר אין את זה בספרייה שאני משתמש WIX. אני צריך נטו את ה JavaScript
תודה חברים -
אתה מתכוון ל
fetch("https://someapi.com/api/someendpoint", {"method": "get"}) .then( (httpResponse) => { if (httpResponse.ok) { return httpResponse.json(); } else { return Promise.reject("Fetch did not succeed"); } } ) .then(json => console.log(json.someKey)) .catch(err => console.log(err));
לא הגיב לי. הגיע ריק
-
@אבי-203 אמר בקבלת מידע מ-API:
https://shutaf.im/php/phone.php?oper=getTotalDonations&cmp_id=640
כן לזה התכוונתי.
בקוד שלך יש כמה הנחות (אולי השארת אותם מהדוגמא)
ככה אמור לעבוד לך מצוייןfetch("https://shutaf.im/php/phone.php?oper=getTotalDonations&cmp_id=640", {"method": "get"}) .then( (httpResponse) => { if (httpResponse.ok) { return httpResponse.json(); } else { return Promise.reject("Fetch did not succeed"); } } ) .then(json => console.log(json)) .catch(err => console.log(err));
תוודא שיש לך את ה
import
הנכון למעלה:import {fetch} from 'wix-fetch';
-
@אבי-203 אמר ב[קבלת מידע מ-API](/post/70289
תודה חברים זה עובד נהדר!
$w.onReady(function () { fetch("https://shutaf.im/php/phone.php?oper=getTotalDonations&cmp_id=640", {"method": "get"}) .then( (httpResponse) => { if (httpResponse.ok) { return httpResponse.json(); } } ) .then(json => $w("#textsum").text = String(json.data.donation_sum_amount )) // .catch(err => console.log(err)); });
-
יש פונקצייה שמוסיפה לזה את הפסיקים כל שלש ספרות? של האלף והמליון?
כמו שיש פונקציות להגדיר את השבר? -
יש פונקצייה שמוסיפה לזה את הפסיקים כל שלש ספרות? של האלף והמליון?
כמו שיש פונקציות להגדיר את השבר?@אבי-203 תמיד תציין אתה בWIX Corvid או בסתם JS.
כעת אני מניח שזה המשך של הנושא הקודם וממילא מדובר בצד שרת (להבא תפתח אשכול חדש)
תשתמש בפונקציה של Intlnew Intl.NumberFormat("he-il").format(2000000);
אם מדובר בצד לקוח תעשה מה שעשית (כי יש דפדפנים שנראה לי עוד לא תומכים בזה) או מה ש@yossiz הציע (בזה אני פחות בטוח).
-
תודה חברים!!
בסופו של דבר זה מה שיצא. זה עובד. מקווה שגם תקין ונכון.$w.onReady(function () { fetch("https://shutaf.im/php/phone.php?oper=getTotalDonations&cmp_id=640", {"method": "get"}) .then( (httpResponse) => { if (httpResponse.ok) { return httpResponse.json(); } } ) .then(json => $w("#textsum").text = formatNumber(String(json.data.donation_sum_amount ))) .catch(err => console.log(err)); }); function formatNumber (num) { return num.toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1,") }
-
@dovid
אוי לא! אני חושב שלא באמת הבנתי.
אני לא מצליח להשתמש בזה פעם נוספת. לדבר נוסף. איפה אני טועה.fetch("https://shutaf.im/php/phone.php?oper=getTotalDonations&cmp_id=640", {"method": "get"}) .then( (httpResponse) => { if (httpResponse.ok) { return httpResponse.json(); } } ) .then(json => $w("#textsum").text = formatNumber(String(json.data.donation_sum_amount ))+" ₪ ", $w("#slider1").value = Number(json.data.donation_sum_amount ) ) .catch(err => console.log(err));
אני חושב שהבעיה שלי זה השורה הזו
.then(json =>
-
שמת שתי שורות,
אז דבר ראשון שים בסוף השורה נקודה פסיק (;), דבר שני בגלל שזה לא שורה אחת אלא שתיים אז צריך להיות בלוק תחום בסוגריים מסולסלות {}, ככה:.then(json => { $w("#textsum").text = formatNumber(String(json.data.donation_sum_amount ))+" ₪ "; $w("#slider1").value = Number(json.data.donation_sum_amount ); }).catch(err => console.log(err));