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
`
Related
Have a php code selecting from SQL with "echo json_encode($array);" at the end.
Want to build a graph on D3.js, so I need to get that JSON from php.
How to make it with D3.request() or d3.json() or other? Please, help me with example.
(Here is guide, but I cant make it yet - https://github.com/d3/d3-request/blob/master/README.md#json)
AJAX request is simple:
$.ajax({
type: "GET",
url: "get_json.php",
data: "FirstName="+ name, //here is the parameter
success: function(data){
var data = $.parseJSON(data);
alert(data.Content);
}
});
In order for your javascript code to access the php script it will need to be run on a web server. A simple way to run a web server if you have PHP installed is:
Open command line
Navigate to the directory with this javascript file and your get_json.php file
Run the command php -S localhost:8080
Now your files are running on a local php web server which allows you to reach the file containing your AJAX request at: localhost:8080/[ajax js file]
var par = 23
d3.json("get_data2.php?par=" + par, function(error, json) {
if (error) return console.warn(error);
data = json;
});
solved.
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" );
i am passing a huge javascript array to php via the $.ajax method of jquery. The Content-Type is JSON and i sent the data as RAW.
In PHP i get the data with file_get_contents('php://input').
Everything is working very well, but on huge javascript arrays, for example 2,5MB, php receives only the small vars and the huge vars are stripped out. Before i send the data with jquery i do a JSON.stringify to send the data as JSON. PHP still will strip out all huge vars in this JSON string and if i get the contents of "php://input" only the small vars are left over....
In the php.ini i have raised up the max_post_size and other stuff, but nothing will work...
Does anybody know how i can handle this right?
thx!
Probably not the best idea but you could split your data, somthing like (not tested, might need a few corrections):
var json = HUGEJSON;
while (json.length != 0) {
var toSend = json.length >= 4096 ? json.substr(0, 4096) : json;
$.ajax({
url: 'server',
method:'post',
type:'json',
data: toSend
});
json = json.length >= 4096? json.substr(4096, json.length-4096) : json;
}
Then find a way to rebuild the data server side until all the data have been send.
I've been trying to pull data from the steam api, and have had no luck because I always get the above error. Here is the code I am using:
var steamurl = "https://api.steampowered.com/IDOTA2Match_570/GetMatchHistory/V001/?key=[keyomitted]&account_id=38440257&Matches_Requested=10";
function populate_api(){
var json;
$.ajax({
'url': steamurl,
'dataType': "jsonp",
'success': function (data) {
alert('success');
json = data;
}
});
}
I omitted my API key.
I have looked at many other posts, and cannot figure out where the problem is. I have tried using Jsonp, regular json, I have also tried using "&callback=?" after steamurl, but to no avail.
The solution for this is to add a local proxy that your jQuery code will call. Your proxy will be server side code (PHP, Python, Ruby, etc) that forwards the query on to Valve and then returns it to your jQuery call. You will, however, have to use one of their supported formats (of which JSONP is not one).
A high level view of what you'll be doing:
Create PHP file that accepts the parameters jQuery will be passing. In this case, it looks like account ID and matches you want to receive. Do not pass the API key, this should be stored in the PHP file
In PHP, build your steamurl using the stored API key, and the two passed values
Issue a call to the Valve servers using this steamurl and retrieve the results.
Return this response to your ajax call
Your PHP will look something like this (and should include more error checking since I am just taking the $_GET values as gospel:
$matches = $_GET['matches'];
$acct = $_GET['accountid'];
$APIKEY = <YOURKEYHERE>;
$steamurl = "https://api.steampowered.com/IDOTA2Match_570/GetMatchHistory/V001/?key=$APIKEY&account_id=$acct&Matches_Requested=$matches&format=json";
$json_object= file_get_contents($steamurl);
header('Content-Type: application/json');
echo $json_object;
Now you can use jQuery to parse this JSON response.
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