How to get json data when url give .txt [duplicate] - javascript

This question already has answers here:
How can I get query string values in JavaScript?
(73 answers)
How to make a simple JSONP asynchronous request in Angular 2?
(4 answers)
Closed 5 years ago.
I have a preblem about get json data form url
example url:
https://books.google.com/books?bibkeys=ISBN:1118691784,OCLC:879947237,LCCN:&jscmd=viewapi&callback=updateGBSCover
this url is give .txt and .txt have json data
I have no idea to get json data in .txt for show in page
Thanks for help me :)

The data is in JSONP format, e.g. JSON wrapped in a callback
More information here: https://developers.google.com/books/
$.ajax({
url: "https://books.google.com/books?bibkeys=ISBN:1118691784,OCLC:879947237,LCCN:&jscmd=viewapi",
dataType: "jsonp",
jsonpCallback: "updateGBSCover"
});
function updateGBSCover(data) {
// console.log(data);
$("#result").append($('<img/>',{"src": data["OCLC:879947237"].thumbnail_url}));
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="result"></div>

Related

Can I fetch JSON data from an URL and assign it's data in an 'window' variable using JavaScript / jQuery? [duplicate]

This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 2 years ago.
So my current scenario is I want to fetch JSON data from an URL, parse it and assign it to a window variable. So the code is as following:
$.getJSON('https://apiv3.iucnredlist.org/api/v3/species/citation/loxodonta%20africana?token=9bb4facb6d23f48efbf424bb05c0c1ef1cf6f468393bc745d42179ac4aca5fee', function(data) {
var id = data.result[0].taxonid;
});
window.speciesId = id;
console.log(window.speciesId);
Is this or something similar possible in JavaScript / jQuery? I'm not very familiar with JS so therefore would be grateful if a workaround can be suggested.
You can use ajax:
$.ajax('http://dummy.restapiexample.com/api/v1/employees', // request url
{
success: function (data, status, xhr) { // success callback function
console.log(data); //data received
}
});
Don't forget to import jquery:
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

How to store a js value in php variable? [duplicate]

This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 3 years ago.
I want to store the value of x in js variable into php variable on the same page.
Short answer is: you can not.
The reason for this: PHP is executed on the server side, and the response is sent to the browser, and (usually) generated as HTML. The javascript is rendered after this, so at this point, the PHP code no longer exists.
Long answer:
You can send a javascript variable to PHP using an XHR request (or more commonly known as an AJAX call). This will be a different request from the one that loads your initial page though. For more information see this: https://www.w3schools.com/Php/php_ajax_php.asp
you can use ajax or
add input hidden with this value and when submit form sent a value
i think in most cast ajax in best.
$.ajax({
url: 'url',
method: 'POST',
data: 'data',
cache: false,
success:function(data){
},
error: function(data){
}
}); // end ajax

Receive data with flask and send data with jQuery [duplicate]

This question already has answers here:
How do I get the different parts of a Flask request's url?
(4 answers)
jQuery posting JSON
(3 answers)
How to get POSTed JSON in Flask?
(13 answers)
Closed 4 years ago.
!! THIS IS NOT A DUPLICATE !!
The question was not how to get an URL in Flask, but how to send data with jQuery to Flask!
I try to send and receive data with python, Flask and jQuery
The problem is that I want the full URL of the website and it is impossible to get it with flask because I make 'POST' requests. So with jQuery, I want to send the current URL.
I don't know how to send data (with jQuery) and receive data (with Flask).
Python/Flask code:
#app.route('/invisible', methods = ['POST'])
def dynamic_refresh():
return jsonify({'somedata': 'data'})
HTML/jQuery code:
<script>
$(document).ready(function() {
window.setInterval(function() {
$.ajax({
type : 'POST',
url : '/invisible',
//I tried to send data from here but it didn't worked
})
.done(function(data) {
console.log(data)
console.log(window.location.href)//the url I want to send
//here I use the data received by the server
})
}, 5000);
});
</script>
Its quite simple, enclose data in JSON array which you want to send through POST request and then retrieve any data from Flask endpoint like this;
var url = $('#url').val().trim(); //get your value from HTML here
var params = {
_url: url,
};
var array = JSON.stringify(params); //enclosed it in json array
$.ajax({
type: "POST",
url: "/invisible",
data: array,
dataType: 'json',
success: function(results){
console.log(results)
}
});

Scraping the url with Jquery [duplicate]

This question already has answers here:
Simple Screen Scraping using jQuery
(7 answers)
Closed 5 years ago.
I want to get data from other url which is product info. I want to scrape all of this data for this attribute:
$('[data-b-for-cart]').attr('data-b-for-cart');
And want to export that to csv file.
Not sure hows this should be done any resource would be helpful.
I think I should use the jquery $.get is that right ?
you can try ajax within jquery to scrape. It is not that difficult
$(document).ready(function() {
baseUrl = "http://www.somedomain.com/";
$.ajax({
url: baseUrl,
type: "get",
dataType: "",
success: function(data) {
//do something with data and save as csv file
}
});
});

How to load the html content of a URL to a variable in JavaScript? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
HTTP GET request in Javascript?
I have a url that contains a XML tree. I want to read that url content, save it to a variable, extract XML tree values, and draw it in a table.
Use an AJAX query, in jQuery:
$.ajax({
url: url,
dataType: 'xml',
success: function(data) {
// Data is your xml content which you can work with
}
});

Categories