Writing to XML from JavaScript using asp or aspx page - javascript

I am writing a bookmarklet that pops up a form and allows users to select xml files. I am reading the file using:
$.ajax({
type: "GET",
url: "http://localhost/products/dataSource.xml",
dataType: "xml",
success: function (xml) {
$(xml).find('node').each(function () {
//get values from xml file
});
}
});
This works well.
My problem is that I want to edit and add to the xml and I understand that JavaScript cannot directly write to the file for security reasons.
I understand that I can write to the xml file using an asp or .aspx page.
Is this still the best way to achieve writing to the xml file and what would the code look like?
I'm looking for the simplest way to achieve my goal.

Yes you're right. You can't do that in javascript since it is being executed in the browser on the client. You should do this in asp.net code behind. You can call the c# method with ajax if it helps. In c# you can do it with XDocument.

Related

How to run php file in javascript without using ajax?

I want to run a php file in javascript when the user refreshes or leaves the page. I'm using a window.onbeforeunload function so if I use ajax, the php file would run without the page reloading. That means the php file would run even when a modal pops up and I don't want that. So I want the php file to run when the user refreshes or leaves the page.
window.onbeforeunload = function (e) {
$.ajax({
type: 'POST',
url: 'myfile.php',
success: function(response){
}
});
}
Vanilla Javascript have no access to filesystem, you can't run php from it.
Using ajax just allows you to get text content from specified url, same result you can get, when put this url in your browser.

Ajax put to add XML entries into XML file

I am trying to add some emails taken from an inputbox into a .txt file present on my webserver. Here is the code :
email = document.getElementById("mail").value;
$.ajax({
url: 'maillist.txt',
datatype: 'text',
type: 'PUT',
data: email + '; ',
success: function(data) {
alert('Should have work yay!');
}
});
but that doesn't work on any browser. :(
I have tried using javascript classic methods but it was a no go as well...
I would need either a PUT or POST method, either jQuery or JS, to be able to do this on internet explorer 8 and up as well as firefox and chrome. Emails should appear in the text file as
email1#cooldomain.com; email2#cooldomain.com; .....
Just so it works with our in-house VBA Macro. :)
Also, could there be a method for dropping data into XML files (aka create a new XML entry with form data)? And also, is it possible to upload a file from client side to server side using jQuery? Because i would need users to fill up forms and drop their data into an XML file, and link up a file they choose with that. That way they could add stuff into the XML themselves and they would show up brand new into the webpage.
Kindoff "reddit" or "4chan" like if you know the references.
Thanks for your time, really appreciated!
You can't post from a browser to a text file on the server side. You need to have some sort of code on the server side that will receive the HTTP PUT, and persist the data to a file local to the server.

JS file in script tag's src attribute downloaded but not executed(from ajax call)

I am invoking an ajax call from jquery like this:
$.ajax({
type: 'GET',
url: 'edit.htm',
success: function(data){
container.html(data);
},
});
The data recevied from the ajax call contains script tags that reference other JS files, for ex: angularjs. Wehn I look at firebug I see that the JS files are downloaded but they do not appear in the Script tab one could debug it.
The JS files are downloaded but not executed.
How do I get around this?
The above ajax call and the container element are present in a html file called info.htm.
And edit.htm(the data fetched from the ajax call) has script tags and other html data.
Thanks.
P.S: If it helps: I can see the JS files being downloaded in the firebug 'Console' tab, however, I cant see them listed in the firebug 'Script' tab.
try with jQuery.getScript("url") for more details refer this
It makes ajax call implicitly.
or try something like :
$.ajax({
type: 'GET',
url: 'edit.htm',
success: function(data){
$(data).appendTo(container);
},
});
I had once a problem with CSS - it had incorrect Content-Type header and was not interpreted by the browser. Can that be the problem? Does your server return text/javascript for the requested script?
May be your requirement is kind of odd because server doesn't return client code.
Best practice is load js file at the start, and you can call required method after completion of ajax script.
However, eval may not be a good practise but this is one solution.
To execute javascript code which is in variable, you need to use eval method.
Please take a look at this reference http://www.w3schools.com/jsref/jsref_eval.asp
hope this helps.

JSONP python script

I've been playing around with the code (HTML2Canvas) from here: https://github.com/niklasvh/html2canvas
It's a client side Javascript tool to convert a HTML page to a canvas element.
Its uses a proxy to fetch the HTML from a remote site, it makes an Ajax call like this:
$.ajax({
data: {
xhr2:false,
url:urlParts.href
},
url: "http://html2canvas.appspot.com",
dataType: "jsonp",
success: function(html) {
This results in the following, when requesting yahoo.com as the sample URL, url being requested:
http://html2canvas.appspot.com/?callback=jQuery162020564090818326575_1311846010895&xhr2=false&url=http%3A%2F%2Fwww.yahoo.com%2F&_=1311846201150
What I want to do is roll my own JSONP proxy which I can point my copy of the code to. Trouble is I have no idea where to start.
The JSONP that is returned (I won't copy it all) begins like this:
jQuery162020564090818326575_1311846010895("<!DOCTYPE html>\n<html lang=\"en-US\" class=\"y-fp-bg y-fp-pg-grad bkt701\" style=\
So the HTML is escaped and wrapped in a callback.
I'd like to create a Python script that works along the exact same lines and generates the exact same output.
Can anyone point me in the right direction for creating a Python JSONP proxy which would generate similar output? It doesn't have to be Python, I'm just referencing that as it's what is currently used.
I put the source up for the python proxy at https://github.com/niklasvh/html2canvas-proxy. Be warned though, the script will still go through changes which will most likely break the proxy.

Client side html markdown conversion

I've been trying to create a client side editor which allows the end user to create content in html or markdown. The user has two tabs for switching between the two. I managed to find some javascript that converts markdown to html, so if a user has been writing markdown and switches to the html tab, the html equivilant is shown. I haven't been able to find a javascript that converts html to markdown, only a python script.
The python script is obviously server side. The tabs are just hyperlinks with script in there. Is there any way I can convert the markdown html when the user clicks the tab?
The currently accepted answer actually tells you to do it on the server-side.
To really do client-side conversion, you could try one of these libraries (in order of popularity, measured by GitHub stars):
marked
showdown
markdown-it
markdown-js
reMarked.js
feel free to try my lib, reMarked.js, for client-side html/DOM > markdown
https://github.com/leeoniya/reMarked.js
the other way around you can try marked, but be aware that it doesn't support some php-markdown-extra features, like parsing pretty tables http://michelf.ca/projects/php-markdown/extra/#table
https://github.com/chjj/marked/
You only have to send the data to the server using AJAX, perform the conversion on the server and then return the results back to the browser. In jQuery this is as simple as e.g.:
$.ajax({
type: "GET",
url: <converter url>,
data: <html>
success: function(markdown_text){
$('#id_container').text(markdown_text);
}
error: function(XMLHttpRequest, textStatus, errorThrown){
alert('Error!');
}
});
Why don't you use WMD-Editor? It has the ability to preview the html.

Categories