How to use this json and show it in browser? - javascript

I frequently use this twitter api website to search. It provides its own api. Now I want to try it but do not know how to do it. My idea is:
Put a search box in html and in onclick event it will retrieve json
Parse that json and count the length of each tweet (only length of tweet text, not userid)
Display each tweet userid, date, link, text (all tweet data) in browser
Length of tweet text is appending to (3) for each tweet
I am new in javascript and json. Will you let me know how to retrieve json and do above?

First, it would help to review the fundamentals of JSON requests. Once you do that, feel free to brush up on jQuery and use jQuery.getJSON() to retrieve the JSON you want from a given URL. Here's some pseudo code:
$.ajax({
url: 'http://tweetscan.com/json.php',
dataType: 'json',
data: { 's' : 'some search phrase' },
success: function (response) {
// Do something with JSON response.
}
});

Related

How to send very big amount of data to server using ajax

I am practicing ajax and want to send big amount of data to server (for instance I want to make tags for a post which can be up to 20 tags). Currently all I do is concatenate each tag with specific symbol between them and then in server I filter it and convert it to many tags again but I don't think that's the natural way. So what is the best way to send say, 30 - 40 entries to server with ajax optimally.
UPDATE (As some of the people suggested I am showing js code example):
$(document).ready(function(){
var tagsToSend = "tag1%tag2%tag3%tag4%tag5%tag6%tag7%tag8%tag9%tag10%tag11%tag12%tag13";
$.ajax({
url: "test.php",
method: "POST",
data: {
tags: tagsToSend
},
success: function(result){
alert(result)
}
});
})
So basically in server I'll just iterate over the given tags string and filter each tag. And I want more natural way.
I think the better way is to sending tags as a json array and not GET parameter. Something like this:
var postData = {};
postData['tagsToSend'] = ["tag1", "tag2", ...];
And inside your ajax config:
data: JSON.stringify(data)
Now, you can get a json in your php file and parse it into php array.
This can help you to have more readable and cleaner request to the server.

Sending data to the backend

I have used AngularJS and I am a beginner .
I have tried to send the text field and drop down value to the back end (Java). I have successfully sent the text with out no problem. But I have failed to send the drop down value to the back end.
When I turn on the debugger mode in browser it successfully showed the text field value which is "firstName":"mike" but drop down value showed me "stream" "". Can you please give me a solution?
You send JSON object by using POST or PUT methods.
sample example will be
$http({
url: 'Home/Index',
method: "POST",
data: user
})
.then(function(response) {
// success
},
function(response) { // optional
// failed
});
You do not need to convert model objects to JSON the angular does that for you.
see the fiddle for working example
http://jsfiddle.net/bkUEu/458/
Use valid JSON format to send data from front-end to back-end,
{"firstName":"mike", "stream":"aaa"}
As per my knowledge,you cannot send object directly,you need to change the object into json.
JSON.stringify(obj);

How do I grab the objects from php Twitter API

So I can get the tweets using Twitter Oauth I can echo the tweets using foreach. But I want to grab the data from the php and bring them to another page.
Index.php contains the json array of tweets.
Then in page2.php I have the code below to display specific objects using $.ajax get from index.php
$.ajax({
dataType: "html",
url: 'index.php',
success:function(data){
//$('#tweets').html(data['text']);
$.each(data, function () {
list += '<p>'+ this.text +'</p>';
});
$('#tweets').html(list);
}
});
But when I try to specify the a particular object to grab like the text I get no results. Do use
data['text']
or
data->text
From observing your current code, you're using the JQuery GET method to send the data to the PHP file.
I am 99% sure that the following should work:
$_GET['list'];

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>

Javascript / jQuery / AJAX - Request string from website

I'm trying to create a function, using an API, to get the definition in french of a word.
I'm using this API:
"http://www.igrec.ca/project-files/wikparser/wikparser.php?word="
+ word +
"&query=def&count=1&lang=fr"
This url returns one definition in plain text of the word entered.
e.g: http://www.igrec.ca/project-files/wikparser/wikparser.php?word=manger&query=def&count=1&lang=fr
How does one manage to get this text? I looked at similar questions, some mention Ajax / xmlHttpRequest but I'm pretty lost.
Thanks
P.S: I don't mind using jQuery or some other technics as long as I understand what I'm doing.
The basic jQuery AJAX call can be done like this ... (using your data).
$.ajax({
type: "GET",
url: "http://www.igrec.ca/project-files/wikparser/wikparser.php",
dataType: "text",
data: {
word: word,
query: "def",
count: 1,
lang: "fr",
},
success: function(data) {
// Do something
}
});
Basically, you are performing a GET, at the URL.
The data type you expect back is text.
The data gets "transformed" into the url format from your question, then there is a success function to handle the data received.

Categories