JSON post using Python requests - javascript

I am using python 2.7 on windows 7 with requests requests module.
I am trying to find the source of some data from a website that uses AJAX/javascript to drive events.
Here is my python post:
result = s.post(url, headers = {'referer': my_referer})
The output from this post looks like this:
{"ADATA":{"COUNT":0.0,"AITEM":[]},"WM":0.0,"CM":0.0,"PC":"","PW":"","NC":14,"RR":false,"RTIME":{"RITEM":[],"COUNT":0.0},"WC":1.0,"CC":1.0,"RW":false,"RA":false,"RC":true}
"AITEM":[] should be populated with all the data I'm after but as you see its not. Also RC: should be false. Or at least it is when I go through my browser
Note that I get "RA":true and "RC":false if I use get instead of post request. I dont know why.
Here is the corresponding post in the javascript server side:
$.getJSON (url, function(data){UpdateStuff(data);});
Disclaimer: I have been programming for about a week now.
This JSON post is in a function that receives no parameters and returns nothing as far as I can tell. data is not referenced in the function prior to the post. I dont really understand what function(data) is. UpdateStuff(data) is another function with a bunch of code that takes data and returns nothing. The code reveals some stuff about the data structure with references such as:
if (data.RA){resetAL();} else {
if (data.RR ){objAR.attr('ref','Y');}
if (data.RC ){objAC.attr('ref','Y');}
if (data.RW ){objAW.attr('ref','Y');}
Which I guess I am failing this logic gate since my data is different than what I see in a successful browser request.
and references to data.ADATA.COUNT and data.ADATA.AITEM[i] etc
I've been told this would be much simpler to write in JS but I have about 70% of this program done in python already and I've never used JS.
Any help is greatly appreciated.

Related

Chrome extension rejected: is it forbidden to fetch any json data from a backend?

so I made a Chrome extension whose whole purpose is fetching certain data from the backend and process it to do stuff on a certain domain that the user visits.
I'm trying to have it published but it's getting rejected, and this is what they told me:
Your item was found to have requested/fetched one or more external scripts. An example of one such instance in your item was backend URL in background.js. Please remove all external fetches including Json type.
(This is actually the last of 3 emails they sent me, they just added a few more words in this part I quoted with every email... Since they send only one per day, it's very frustrating...)
I use jQuery.ajax in my background script, and after searching with google I found out that by default it tries to process json requests as jsonp requests (I'm not 100% sure though...), so I've set the jsonp property to false in every ajax call in my code. My extension still got rejected today, and they didn't send another email, so I'm just gonna guess they really did mean that I need to remove that call that fetches json from my backend.
Here's an example of an ajax call in my code:
$.ajax({
url: backendUrl + '/theendpoint',
data: {
paramName: 'paramValue'
},
dataType: 'json',
cache: false,
jsonp: false
})
I'm pretty sure that I'm supposed to be allowed to do it. I've also searched to make sure, and other people do it too. So, what could actually be wrong?
I know it's hard by seeing hardly any code, but there's too much of it and the problem is just here in the ajax calls. And I can't post here the content of my manifest file.
I did add my backend to the permissions in the manifest. Do I have to add it to the content_security_policy too, even though I'm just fetching json from it, and not scripts?
Thanks for any help.
Edit: side question: is it mandatory to provide a physical adress and a link to a privacy policy in my developer account? If yes, could that be the reason why the extension keeps getting rejected? (Last time it got rejected, they didn't even send me an email)
(I'm not sure if I should post this as an answer, but)
Today I tried insisting again saying that json isn't a script and that I was supposed to be able to fetch it from my backend. I don't know if it was a coincidence but right after sending the email, I received another one saying this:
Thank you for reaching out to us.
Upon a subsequent review, we’ve reinstated your item and it will be available in the Chrome Web Store within 30 minutes.
Thank you for your cooperation,
Chrome Web Store team
I also must add that I did use this support form to ask for help. Maybe that's what actually did something.
Moral of the story: if your extension is getting mistakenly rejected, keep insisting and explain what you did and why it's valid...
Now, I've only gotta understand why it got immediately taken down from the store...
Edit: My extension was also taken down by mistake, they reinstated it after I used the support form to ask for the reason. So yeah, use that support form, it actually gets things done.

$_POST responding back to webpage, and how to deal with data on a dynamic web page [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I am fairly new to php so this functionality stumps me, I'll give you a picture for context. layout of application for reference image
So I can query my data base and set each bit of data to an individual variable (maybe not the best way for this situation?)
But I don't know how to $_post that data (in variable form... :) probably bad idea, and I'll have to set the data as variables where ever I work with the formulas and only send information over ajax) back to the displayed page (note: I don't want my page to ever have to refresh)
And I foresee another problem, and its due to lack of knowledge and google failing to answer my questions, how can I have php Run my algorithms and keep the original data in the page every time my users change one field on the page, I want them to all run on the server and not in browser so I'm planing on having ajax send all the data including the changed data to a php file every time they change something and have it reset the information on the page after it runs its formulas. i have been studying $_post and get and request ect. and not been able to mentally layout how the transmission of data would be the most efficient and easiest to work with.
Sorry for the ramble, I hope for some constructive criticism, solutions and explanations to a newbie at php and thank you for any and all help.
HI it works like this in jQuery
$.post('url', {input}, function(data){
});
The javascript will make a web request, same as if you put url in the browser, right ( if it's $.get that is, obviously we cant send post data using the browser url bar, however you could go to the page just without the post data ). Now because it's AJAX anything returned goes into data. It's asynchronous, which means your JS wont wait for the request, for example if you put
$.post('url', {input}, function(data){
});
alert(data.result);
It wont work, for 2 reasons, one is scope ( data is a function input parameter not accessible outside the function ). The other is that the alert will fire before the post completes, even though it is written after the $.post request. Because of this, you have to use the data in the call back function.
Now on PHP side where ever that url is it gets ran just like you went there normally. Essentially there is no difference to the server its AJAX or a browser request. It's just like submitting a form. The interesting things you can do is return your data as JSON. by using the application JSON header and by using json_encode() in PHP. This is essentially JavaScript String Object notation. ( not sure if I remember that right ) but that is what it is.
There is no magic in the request. Most people don't really understand that anything the server returns is only text. Be it a PDF file, a JPG, a webpage, anything its only text. JSON allows you to keep the structure of your data, such as an array. It's a special format of text that JavaScript understands as object and arrays.
I explained this to one of my Junior Developers the other day. That if you set the correct headers you can generate CSS files with PHP, images etc. Because it's all just Text. The web is very simple. You only have get and post for requests and text as a response. That's it for the most part.
Anyway,
Right, so now in PHP ( for a quick example )
header('Content-Type: application/json');
$a = array('one'=>1);
echo json_encode( $a );
With json_encode() it becomes ( this is the response sent back from the server )
'{one:1}'
And in JS on the client side ( inside the $.post callback ) its
data.one;
We can take this data in the callback then you can just use some basic jQuery to replace the values of inputs or content of HTML tags with your returned data.
$('#input').val(data);
$('#htmlElement').text(data); // or data.one - whatever you had in php array keys.
Make sense? Above, we take a PHP array use json_encode(), to make it a JSON string, and then (with the correct header ) we can access that normally in JS using its dot syntax. Remember what I said above about only returning text from the sever, this is why we have to convert it to a JSON string. The dot in JS is like the -> in PHP. You could also use data['one'] which is even more like PHP, but that is technically not the correct way if you know what one is.
The process flow is also simple, just like we only have post get, and text. We only can make requests from the client and responses from the server. So it always goes
Client Request -> Sever Response -> Client receives response.
We cannot for example call the Client from the sever.
Sever Call-> Client receives
This doesn't work without things like NODE.js or a socket server.
For reference ( about json responses )
Returning JSON from a PHP Script

create file in Ajax javascript

I was wondering if there was any way to create something like a txt file in Ajax. I'm using this to save logs created by the user on my site, this is the link if its helpful: site. If this cannot be done with ajax can it be done with cookies? Or would the log be to big for cookies? Thanks(sorry if this is a duplicate, some I looked around and everything was about loading/reading from a file).
AJAX is just making asynchronus HTTP calls on the browser.
So, can you create a file just making an ajax call? No. You would need to setup somekind of script on the server that will handle the file creation and writing and call that script with AJAX.
The short answer is - no. You can not do this DIRECTLY with AJAX.
The longer answer is - what you are looking for should be done using a database and SQL. Not that PHP couldn't do it directly - but a good database is better able to handle this problem.
AJAX is mainly used to just send and receive messages, files, etc... It isn't for storing anything. With that being said, the steps would be to have a small PHP script that just reads and writes to a database and make that database be a log file. So your SQL database (I'd just call it log) would have two entries. These are the date the log entry went into it and the entry itself. You could also throw in last_accessed if you want or other fields you think you might need. Your PHP script, in its simplest form, would open the database and, depending upon whether you send it a "GET" or "PUT" command it would read from or write to the log file. If it writes to the log file, it sends back an ACK or NAK (meaning everything is ok or the program had a problem). If it is reading from the log file, you supply how many lines you want to read and the PHP script either sends back the lines or a NAK again to signify something went wrong. You can also replace the simple NAK with the actual error if you want but then your customers are seeing the error. Better to have just a generic "There has been a problem, we have contacted our system people and the site should be back up shortly" kind of message. Save the actual errors for you to see via e-mail.
That's it. You should be able to write this in about ten to fifteen minutes and have your log file readily available to you in no time.

AJAX calls - where does logic go?

This might not even be an AngularJS question and could just be an AJAX question. I'm new to the "developer" side of the frontend so bear with me.
When making an AJAX call to fetch JSON data, where does the logic behind what data is returned and viewed fall? In my mind, there would be a couple of possibilities and I want to understand which is the proper choice and why.
Let's use an example of searching and playing a Youtube video.
The logic could fall to the backend (controller), where the JSON is rendered based on some logic to give you a JSON file with exactly the right data. i.e. you search "cat videos" and when making an AJAX call, the JSON file you pull has been rendered to be only cat videos.
The opposite end would be that the Angular controller has the logic. This would imply that all data is called (cat videos along with everything else... music videos, funny videos, tutorials, and so on) and then sorted through on the client side. This, to me anyway, would be more inefficient / slow for the client, so doesn't seem to make sense. I suppose still might do some filtering of the data on the client side though. So, maybe a search for "cat videos" wouldn't return ALL videos, but definitely all cat videos and any filtering based on, say, # of views, video length, and so on would be done on the client side (vs. calling the database again for a "new" set of videos).
Not sure if this is accurate, but could you have logic in your factory to return only a portion of the data? However, I believe the entire JSON file would need to be rendered, but only portions would be returned. I guess depending on where the JSON file renders (i.e. backend or frontend) this could be similar to either option #1 or #2.
Or maybe I'm misunderstanding things entirely and the way this works is entirely different!
I'm basically looking to figure out how the scenarios of 1. user searches a term and results are shown, 2. user clicks a search result and now more detailed data of the result is on it's own page. And how this ends up working out. I'm looking for help with AngularJS, but I think this ultimately an AJAX question (single page app or not) more than anything.
There's a few critical concepts you may be confused about.
First. JSON is not a file, it's a format, more simply, a type of string. It's really good for collapsing arrays and storing address-value pairs, so a lot of data flies around in that format. Strictly speaking, they are JSON objects, but they're a lot like strings and arrays. It looks like this, if I remember correctly:
{ "name" : "john doe", "pet" : "dog", "hobby" : "parasailing" }
Second, AJAX is a request to the server, made from the client (the browser) after the original page has loaded. That is, you type in 'youtube.com' and the youtube server receives the request and sends a big pile of HTML back to your browser.
You watch your video, make a rating, and the browser doesn't reload the page but instead sends a separate request back to the youtube server with your rating. There's a parameter in the request that says "send it to ratingspage.php". This request is AJAX.
Now, the logic happens (server-side). ratingspage.php receives your request. It contacts the databases, updates or fails or whatever, and sends back a response to your browser. This response may be in JSON format.
Finally, your browser parses that response and updates the DOM (HTML document) as appropriate.
At this point, it's worth noting that if the logic happened on the client-side (browser), the user could see it - this is a security problem! So, sensitive operations should be carried out on the server side, where you can test and sanitize the request data.
In summary:
AJAX is separate from the initial load event.
Information sent is gathered from the client browser
Logic happens server-side
Logic can use whatever language the server understands (PHP, Java, Ruby, etc.)
Information is returned to the browser
Information sent and received may use JSON format
Everything client-side happens in Javascript
Here's a bare-bones ajax request (done in Javascript) with comments. This has no exception handling, state checking, or anything so don't use it! But it gives you the basic idea.
// Make a new request
var req = new XMLHttpRequest(); }
// Requests will have various states depending on whether they're processing,
// finished, error, etc. We'll assume everything went OK.
// We need to establish a handler before the request
// is sent so it knows what to do.
req.onreadystatechange = function() {
// Here's what the server sent back to the browser
alert(req.responseText);
}
// Using the GET method, set up some parameters
req.open("GET", "somelogicpage.php?blah=blee&bloo=bar", true);
// Send the request
req.send(null);
Server-side, somelogicpage.php may look like:
<?php
if ($_GET['blah'] != 'blee']) {
// This is the response text!
echo "Sorry, you need to blee when you blah.";
}
else {
// (or this)
echo "I'm ecstatic to report nothing is wrong!";
}
?>
Your alert(req.responseText) from the handler function in the previous Javascript will say whatever the PHP has dumped out.
So yes, you can use whatever portion of the request you like, and return whatever you like. Javascript kicks bleep.

Does d3.json() support authentication? If not, what other JavaScript options are available for JSON retrieval?

I am developing an application that needs to gather information from GitHub, so I began looking at their API. My initial thought was to use the d3.json() function to get the data (because it's simple and has done good things for me in the past), but there doesn't appear to be a way for me to authenticate with that function. For example, $ curl -u "username" https://api.github.com is given as an example of basic authentication (from GitHub API--obviously they use curl for their examples).
So, is there a way to do authentication with the d3.json() function? And if not, what are my other options to get the JSON with JavaScript?
Thanks!
Edit:
I'm experimenting now with using jQuery's getJSON method as shown here, because I started getting the error "XMLHttpRequest cannot load url Origin url is not allowed by Access-Control-Allow-Origin." Of course, the switch doesn't help with the ability to authenticate, but I can at least get the public data (which is most).
Edit #2:
Has anyone experimented with michael/github or fitzgen/github-api? I'm going to start looking into those.
If you have a PHP script which echos JSON, you can do all authentication server-side. Note that you can also send a GET request to your PHP script, so the way you call your script can be dynamic.

Categories