How to get JSON from php file using get()? - javascript

Below is the code from where I am trying to fetch JSON from data.php file. What is the mistake I am making?
I am using XAMPP here to fetch the data. The path is correct. I also tried using an absolute path, but no luck.
Opening the absolute path in the browser fetches the array of data I am trying to echo here.
//Get data, and render chart
$.get("/web1/chart/data.php", function(data){
//Get data
var parsedData = ParseData(data);
//Render chart
InitChart(parsedData);
});

In javascript, you no where mentioned the data type is JSON, Please try to use $.getJSON( instead of $.get
Otherwise specify the data type as JSON in your get request.
$.get("/web1/chart/data.php", function( data ) {
alert(JSON.stringify(data));
}, "json" );

Related

Google spreadsheet showing undefined values when submitting through an AJAX request

I'm trying to send the data I gathered from my web app to a google spreadsheet.
I'm using the script from Martin Hawksey:
https://gist.github.com/mhawksey/1276293
I've set it up and did everything as shown in the manual. And I am getting data back, but it's showing up as undefined values:
This is the code I use to send the JSON string to my spreadsheet:
function sendData(){
var url = 'https://script.google.com/macros/s/AKfycby3SUJvfEjdHWVoEON0L5hN4uXod8M4Jv1LAIWH3Ny16MIUz9o/exec';
var data = JSON.stringify(member);
$.ajax({
url: url,
type: 'GET',
dataType: 'json',
data: data,
success: function (response) {
console.log("succes! I sent this: " + data);
console.log("got this back: " + JSON.stringify(response));
},
});
}
This gives me a success message, it even tells me which row it was placed on.
This is the JSON string I'm sending:
{"Voornaam":"Name","Achternaam":"Name","Mail":"x#x.com","Verjaardag":"0/0/0000","Foto":"https://graph.facebook.com/xxxxx/picture?width=1020","School":"School X","Richting":"Course X"}
I even checked this JSON with a JSON parser online and it didn't return any errors.
For starters, I'm not entirely sure how to check which string I'm receiving at my spreadsheet. If it's still correct when it arrives. I tried logging it, but can't seem to get any response from the google logger.
If anyone could point out what I'm doing wrong, you would be greatly appreciated!
The Web script expects a JSON object. However, Ajax call is made with a string using the stringify function
var data = JSON.stringify(member);
Modifying the script to make the GET call with JSON object as is resolved the issue, like so
var data = member;

Getting json data from url instead of local

I have a searchbox that is working with a local Json data (in the same file) using a var but now I would like to use with an external url json file in the same way that it works before.
What I have now:
var data1 = [{"test_id":"1","test":"test","test2":"test2"}];
What I'm trying:
var data = $.ajax({
dataType: "json",
url: 'myjsonurl',
data: data
});
I've tried with getJSON and with other some recommendations but are not working as I expect, in the image you can see that I'm having the data in both ways but quite different and is not working in the searchbox.
Image: https://i.stack.imgur.com/kWKkz.png
You can find the searchbox here:
https://www.js-tutorials.com/javascript-tutorial/live-search-json-objects-data-using-jquery/
Is there any idea to get same data as I have now from an external file and use it into a var as the searchbox do?
Any ideas?
Thanks in advance,
The way you are doing is, what you are getting is XHR Object.
To get proper JSON response and to work on it, you can write a success function and handle response data there or just use getJSON.
For example:
$.getJSON('https://gist.githubusercontent.com/GeekAb/027c70bd21d006027cd91f538ae4694e/raw/68fda14df1b9fbf6835a02639aa210e88826d19d/SampleJSON',
function( data ) {
console.log(data);
console.log(data[0]);
console.log(data[0]['employee_age']);
});
You can check this Bin link https://jsbin.com/cixezewisi/edit?html,js,console
First off, you have a lot of options when it comes to importing data from an external file (some easier than others). The following list is from another stackoverflow post:
ES6 import
Node.js Require
Ajax
jQuery
I suggest checking out the link, as the poster gave great detail on these options.
Anyways, it looks like you are trying to implement this with Ajax. You basically got it down correctly. Based off the image, what your issue is that using ajax has stringified your data, which is expected. All you have to do is:
var data = $.ajax({
dataType: "json",
url: 'myjsonurl'
});
var parsedData = JSON.parse(data)
This will parse your data into the JS array you are looking for.

Read single data in csv data downloaded from url

I'm trying to fetch the data from Yahoo! Finance to get value for currency exchange with this url assuming I'm getting currency exchange for USD to EUR in javascript
http://download.finance.yahoo.com/d/quotes.csv?s=USDEUR=X&f=l1
in the csv, I would receive just the one value I need. Eg. - '0.8994'
Now, in same javascript file, I want this value to be pass into a variable
var USDEUR;
at this point, I'm not sure how to pass the value from the downloaded csv. Could someone enlighten me how to do this properly?
Thanks in advance
It seems you don't actually need to parse this file as a csv, it's basically a plain text string? Assuming jquery:
var USDEUR;
$.ajax({
type: "GET",
url: "http://download.finance.yahoo.com/d/quotes.csv?s=USDEUR=X&f=l1",
dataType: "text",
success: function(data) {USDEUR = data}
});
EDIT: It seems the url has access control issues, you may not be able to do this with a XHTTP request from the client. Is this data meant to be accessed as a public API?
Use JQuery Ajax
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script>
var USDEUR;
$.get("download.finance.yahoo.com/d/quotes.csv?s=USDEUR=X&f=l1", function(data){
USDEUR = $.parseXML(data);
}, "xml");
</script>

Make JSON php request from Firefox addon

i'm writing a Firefox addon on sdk, i need to use in my main function an array. this array contains a list of data located in my BD. that’s why im using a php file to connect the BD and echo data in JSON format . then i call back the php file using request function and finally parse the JSON to an array. (my problem is how to request the php file and parse the json to array ) i tryed
var Request = require("sdk/request").Request;
var function = Request({
url: "file.php",
onComplete: function (response) {
var arrray = response.json;
}
});
but that didn't work :/
i tried a lot of thing too .
could someone help me with the right function to request the php file then parse the json to array
`

GetJson jquery doesn't run success action

I'm using this code to make an Ajax request to a JSON file:
var queries = $("#myform").serialize();
$.getJSON("/files/json.php?" + queries,
function(data){
alert(data);
}
);
But it returns no errors and the alert is not performed. The PHP files send data using the
header('Content-type: application/json');
And if I open the same query within direct URL it returns the JSON data, so the problem is jQuery.
The network tab shows:
json.php?(my queries parameters and values)
/files
GET
200
OK
application/json
jquery.js:9597
How can I fix this problem?
Try this,
var queries = $("#myform").serialize();
$.getJSON("/files/json.php", queries,
function(data){
alert(data);
});
Second parameter for getJSON is actually form data which you have to pass to the php page

Categories