$.getJSON() call from express static html file doesn't get data - javascript

I am using the express module as the basis for my node.js server, and set up a static middleware as follows:
self.app.use(express.static(__dirname));
Within the root folder I have an html file that includes the following url to a php script on my remote server (completely different to the server hosting the node.js application) that returns jsonp data (having converted from xml data from the dataprovider):
var strURL = 'http://example.com/jsonp.php?callback=?&url=http://dataprovider.com/1.4/?arg1=xyz;arg2=abc';
And then a jquery getJSON call to actually get the json data:
$.getJSON(
strURL,
function (jsondata) {
// do some stuff with the json data
}
);
But when I load the html file that is being served from the static node.js folder, no data is returned... the code never reaches the jsondata function.
However, loading the very same html file placed on a "normal" server, the data is fetched just fine, and also if I load the strURL directly, the data is returned OK.
I suspect that this has something to do with cross domain issues, but for the life of me I can't get the page to work within the static node.js server using express. I've tried various solutions out there, but am now thoroughly confused and frustrated!
Any help would be welcome.

Have had cross domain issues with JSON in the past and got round them by using JSONP. There is a good explanation and tutorial at:
http://json-jsonp-tutorial.craic.com/index.html

Related

how to access database object from another server in client sid

I have working on project that using Django.I have render some data as json format with httpresponse in url like this "1.1.1.1:8080/data". and get data with javascript and AJAX in client side, I should mention that this server have no access to intenet and it work in internal network of our company. this work prefect. but another server like 'example.com' want to render my html and i manage it, this work well but the data "1.1.1.1:8080/data" do not load and return error on pinging.
so is there any solution to make access my django app load data on 'example.com'.
best regarads

Please clarify how to use Framework7 ajax (to trigger server side processing)

please can someone clarify what I need to do in a framework7/node-js app to trigger a server-side function from client-side? I want a server side script (coded by me) to compile a json file and send me back the name/url before I request that new json file from server side to client side.
This link shows "Request/ajax" in framework7 documentation [1]: https://framework7.io/docs/request.html
But there is nothing that I could find about specifying a response.
I can use the below function to get a static already existing json file/data from the server:
//below code placed inside 'pageInit' block
//in the code block 'var app = new Framework7({ })'
//in app.js file
app.request.get("/assets/datafile.json", function (data){
//turn json data into a javascript object
var mobj = JSON.parse(data);
}
I've tried to use node_modules, but the function 'require(module)' which is used to import modules is not available to use from client side files...
I selected 'include bundler' in the npm set up of the project directory, and now I'm working with 'vite'. So I can't find a server file to edit if needed.
I've become confused as how to fetch dynamic data in this situation. I've read online to use a XMLHttprequest on a server side API, I've read you have to wrap the server side code in a 'route', I've read you have to change the server file code, I've read you can use a php script, but I haven't been able to make this thing work with Framework7.
If anyone knows how to do this, please give us a hint. Thanks.

Electron - download .zip file using ajax?

I'm trying to build an Electron-based app, mostly using code from my existing web app. The electron version connects to my server and often relies on that online content. I am using Ajax requests (using Jquery) handle things like the user logging on, and a php session is created, which is required to access most of the content.
I am now trying to get Javascript to automatically download a .zip file and save it to a location, without the user doing anything. I failed to do this using an Ajax request, so have tried to use the Node.js 'request' module. Then however, it wouldn't download the file because it was not authorised (because the request creates a new session, different to the existing, logged-in one).
How can I get something like the following to work?
const fs = require("fs");
...
$.ajax({
url: "my-server/file.zip",
success: function (data) {
fs.writeFile("local-file.zip", data);
}
});
Note - I think it failed due to some issue with the way the downloaded data is encoded, but don't understand exactly what the problem was.
Alternatively, is it possible to use the existing ajax session in the request module, and download it that way?

How can I retrieve data from an external XML file and place it in an HTML file?

I am trying to retrieve data from an XML file that is not located on my site's server, and then use that data fro various things, such as charts. Here is one example: http://forecast.weather.gov/MapClick.php?lat=40.78158&lon=-73.96648&FcstType=dwml. This is an XML file with the weather data for central park. I want to retrieve the data that is in the <value>tag, which is in the <pressure> tag, so I can create a graph with barometric pressure. I would prefer to do this with JavaScript, but I don't think it's possible to do so when the file isn't on my server.
Note: I do not want a different solution to retrieve the pressure data from somewhere else, because I want to retrieve other pieces of data from other XML files as well.
There's an interesting article about using Yahoo! Pipes to transform Xml weather data to JSON and use the result in a web page without need for any server side stuff (PHP, curl, etc.).
EDIT
Being new to jQuery myself, I a had to dig a little more to find out that (almost) everything described in the first article can be condensed down to
$.getJSON("<your Yahoo pipes url here>&_callback=?", function (data) {
alert(data.value.items[0].data[0].parameters.wordedForecast.text[0]);
});
using jQuerys builtin JSONP.
Pitfall!
Beware that Yahoo expects the callback url param to be named _callback
Nice summary on Cross-domain communications with JSONP which helped a lot to come up with this answer.
If your javascript code is on a server (as opposed to a mobile device), have PHP code load the xml, escape it and insert it into the HTML page. Then you just have to grab that in your code and process it with DOMParser.
You could use curl to pull the data to your server and act on it from there.
curl -o data.txt "http://forecast.weather.gov/MapClick.php?lat=40.78158&lon=-73.96648&FcstType=dwml"
This will give you the information in a file called data.txt. You could then either parse it server side and then just give the bits of data needed, or make the whole file available to your client, since they are both now in the same domain.

Need Help With Getting Cross Domain XML With JavaScript

Alright, so I'm building a web app that provides music information (i.e. info on artists, albums, songs, etc.) and for the info source I'm using the MusicBrainz API.
Now, I'm trying to load the data from an API call and process it, with jQuery. This is the code I'm using:
Code:
queryString="http://musicbrainz.org/ws/1/artist/?type=xml&name="+qry+"&limit=10";
$.ajax({url: queryString, dataType: ($.browser.msie) ? "text" : "xml", success: function(data){
alert("success");
var xml;
if (typeof data == "string") {
xml = new ActiveXObject("Microsoft.XMLDOM");
xml.async = false;
xml.loadXML(data);
} else {
xml = data;
};
...
With 'queryString' being the URL string for the request, and then I'd proceed to read the data out of the 'xml' object. Fairly simple.
However, this is where problems arise. The code works flawlessly when running locally on my computer, but does not work at all when I upload everything to my web server and try to run it there. I did some reading and have discovered that AJAX calls can't be made across different domains, due to security issues.
So I've read through numerous solutions, but almost all require either something with PHP (which I have absolutely NO knowledge of) or grabbing the data in JSON format (which apparently isn't subject to the same security restrictions). However, my main problem is that the MusicBrainz API does not return data in JSON format (in fact the only format it returns is XML).
So in any event, I was basically just wondering if anyone could give me some help or pointers on if and how I could grab that remote XML file using only JS/jQuery. Or, point me toward another method that could be accomplished by a complete PHP noob like myself.
Thanks for any help!
You require something on your server side to proxy your request to that other server. A URL that looks like:
/proxy?url=http%3A//musicbrainz.org/ws/1/artist/%3Ftype%3Dxml%26name%3Dexample%26limit%3D10
If PHP is available on your server, you can Google to find a generic PHP proxy script.
EDIT Here is an example of very simple PHP script that will retrieve a specified URL:
<?php readfile($_GET['url']) ?>
Note that you won't be able to POST any data to it, or specify a Content-Type. This is the most basic proxy required for very basic needs.
I understand that JSON is not an option right now but still, here is the explanation of why it can work for cross domain requests.
JSON being Javascript, it can be queried using the <script> tag instead of XMLHttpRequest. Since the <script> tag does not have the same restriction for cross domain request, it is possible to retrieve the JSON content this way.
This technique is called JSONP and is implemented in jQuery in the getJSON function.
If you don't want to setup your own proxy server, check out my response here: use jsonp to get xml cross domain

Categories