Hi
I am writing an application where I want to post data after clicking send button, it will post data to some web-action and gets processed, after processing it will be redirected to some other jsp page but I want to be in the same page from where I click send button. I am not sure of XMLHttpRequest method.
Any ideas to get this done highly appreciated.
If you wanna do using Java script and Ajax (As you have in your question tags) then Try following:
function fun() {
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
} else {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
var url = "Serv?req_id=1";
xmlhttp.open("POST", url, false);
xmlhttp.send(null);
var respo= xmlhttp.responseText;
document.getElementById("some_id").innerHTML = xmlhttp.responseText;
}
Call fun() on onclick event of send button.
Hope this helps.
You can use jQuery.ajax() - it's a convenient wrapper for XMLHttpRequest.
You can use jQuery. It will save some time. It is cross browser compatible (i.e. hides the cross browser compatibility techniques from you).
http://api.jquery.com/jQuery.post/
http://jquery.com/
You can issue a 302 request back to the page you came from as the response from the POST request. If you don't want to POST at all I can show you how to do this through AJAX.
The reason you want to use the GET->POST->GET is so that if the user hits the back button it doesn't re-post on you.
Here's a great beginner's tutorial on what you're looking to do (with jQuery):
http://net.tutsplus.com/tutorials/javascript-ajax/submit-a-form-without-page-refresh-using-jquery/
A 302 request back approach seems to be good for this, If you were using ajax or XMLHttpRequest object, it wouldn't be that problem, but here you can set redirection header to redirect back on the same script that process your query-string.
However you cannot do post without a redirection.
Passing values from one jsp page to another jsp page
I know this is a bit late but for the sake of sharing knowledge, I found a good article here: http://net.tutsplus.com/tutorials/javascript-ajax/submit-a-form-without-page-refresh-using-jquery/
On it, is a very thorough example of how to do what you are asking for.
This is done, as mentioned above, with AJAX/jQuery, this tutorial makes use of that.
Related
I'm teaching myself some web development on the side, and just for fun I'm trying to build a small e-commerce website for my gf (She might actually use it for a business idea if I don't get side tracked and not finish in time)
So I've managed to build a basic front end with html, css and javascript... The 1st hurdle I've run into is saving basic user details to a database, could someone please guide me in the right direction on what would be the quickest and easiest way to accomplish this.
The only other programming language I have a decent grasp of (certification level) is Java
Thanks in advance
Nowadays, the best way to store user details is probably to NOT store them at all lol. If you can use any third party authentication like Facebook or google, i really think you should give it a try. If you really feel like doing your own login/sign up. It's gonna be in the back-end. Using PHP is easy if you don't have any experience in that kind of stuff.
This is an example of something not too bad
hope it answers your question. If not, let me know and i can give you some more details !
This question can not be answered in a few sentences. But have a look:
Javascript is executed on the client side while PHP for example is executed on the server. You need to send a so called HTTP Request to your PHP server using javascript. This can be done with jQuery via ajax() function or using native JS like:
var xmlhttp = new XMLHttpRequest();
var url = "myTutorials.txt";
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
var myArr = JSON.parse(xmlhttp.responseText);
console.log(myArr);
}
};
xmlhttp.open("GET", url, true);
xmlhttp.send();
http://www.w3schools.com/json/json_http.asp
but i strongly recommend u to use jQuery:
http://api.jquery.com/jquery.ajax/
What happens during such a http request? The Client (your PC) sends data via an URL to the webserver and u receive it in the response. (have a look at the response in your development tool)
greetings
if(window.XMLHttpRequest) {
request = new XMLHttpRequest();
} else {
request = new ActiveXObject("Microsoft.XMLHTTP");
}
request.open('GET', 'foo.php?bar=' + baz, true);
request.send()
I know that this can be done with PHP, but I'd like to know if/how it can be done with aspx (i.e. request.open('GET', 'foo.aspx?bar=' + baz, true);). In the example above, the AJAX call is being made in javascript from a plain old HTML page (not PHP), but it can interact with PHP.
Can the same thing be done with aspx? If so, do I need to target the code behind file or not? Say that the aspx is only there to respond to requests like this, is there something to put in the aspx file that automatically passes the GET to the vb/cs file?
Any help would be appreciated. I'm just more accustomed to working with PHP, but my current job is primarily a VB shop, so PHP comparisons are welcomed. Thanks.
Aspx was not meant to work this way.
You will need to catch the ajax call on the Load event of the page, and then Response.Write whatever you want to return as a string. Then you have to set the Response.ContentType and then you need to end it with Response.End.
It is complicated and a bit of a hack.
Your best option is to start learning asp net mvc which will most likely work similarly to any php mvc framework.
Ajax just means "Making an HTTP request from JavaScript without leaving the page".
There is nothing special about the HTTP request.
The server can generate the response in any way you like. Static files, Perl, JavaScript (via Node), PHP, ASP.NET, any way you like.
So I am trying to write a script that will go out and edit the html for all of my tumblr blogs, so i dont have to do it manually.
The problem is that tumblr doesnt really have a form that i can edit, and then submit through JS.. they have a very strange way of compiling the custom html and submitting an ajax request.
So, i was wondering if there was a way to intercept the ajax call before it goes out, edit a field in the data, and then make the submission my self?
I tried something like this:
var submitted = false
$("body").ajaxSuccess(function(evt, request, settings){
j = $.parseJSON(settings.data)
j.custom_theme = "PUT HTML HERE"
if (!submitted)
$.post(settings.url, j, function(data){
console.log(data);
submitted = true;
})
})
But i got a 403 forbidden error.
Does anyone have any ideas?
I'm not sure exactly what you're trying to do, but must this be done with a custom script? It sounds like this will only be used by you, so an extension should work. If so, there is a very useful Firefox extension, in the spirit of Firebug, called Tamper Data. This should be able to do what you want.
It allows you to monitor each request made by the browser, and you can turn on an option that allows you to look at, and edit, every single request before it gets sent.
Well, it's pretty obvious that ajaxSuccess isn't going to work in the way you want it to.
Assign the function which sends the AJAX request a new name and overwrite it. Call the previous function at the end of the new function, i.e. after manipulating the data the way you want.
I need to find a way to write ajax responses to a file. The responses are XML strings, which is more than fine by me.
What I would like to do, is click on something in my webpage, and save the XML that is returned to a file.
But since I know, that Javascript can't access local files by itself, it is also possible to just send the data on to another server, where PHP would take care of this.
Now the place where I'm stuck is the javascript and the interception. I know, that some of this can be done using greaseMonkey in Firefox. If so, how? Thanks!
Edit: Some explaining.
The script that creates the output is not written by me.
Yes, I could see the data in Firebug, seeing is one thing. I need to interpret the data
There are a lot of requests going on here. About 1 every 2 seconds, so copying them by hand isn't an option.
Still, help?
You should provide more details, a link to the target page is best.
Is the page using jQuery?, Some other library?, or custom XMLHttpRequest() calls?
Anyway, a simpler approach may work, try it first...
If the AJAX data is being written to the page, attach a DOMSubtreeModified event listener to the container element. Something like:
document.getElementById ("ContainerID").addEventListener ("DOMSubtreeModified", YourFunction, false);
function YourFunction () {
//--- Get the target node's inner HTML and send it to our server.
}
Note that DOMSubtreeModified events work fine in FF and Chrome, the two main browsers for Greasemonkey.
If the data is not being written to the page, then the best way to intercept the AJAX depends on if the target page is using a library like jQuery.
A generic way to intercept AJAX can be seen in this SO question (and others).
As you said, once you have the data, to automatically write it to a file, use GM_xmlhttpRequest() to send it to a server that you control.
Why cannot you do it like this?
Save AJAX response to file on the server side and then provide a link to it, so it can be downloaded.
Firebug will also help, you can view in very convenient way each response in few formats, and eventually copy/save it.
Use a normal (non-AJAX) request and add a Content-Disposition: attachment; filename="foo.xml" header to the response.
If you're just going to save the XML, why are you using AJAX? Just set location.href to the location of a PHP script that sends a "Content-disposition: attachment" header and gives the XML in the response body. AJAX seems totally the wrong tool for the job.
How do you get around this Ajax cross site scripting problem on FireFox 3?
If you're using jQuery it has a callback function to overcome this:
http://docs.jquery.com/Ajax/jQuery.ajax#options
As of jQuery 1.2, you can load JSON
data located on another domain if you
specify a JSONP callback, which can be
done like so: "myurl?callback=?".
jQuery automatically replaces the ?
with the correct method name to call,
calling your specified callback. Or,
if you set the dataType to "jsonp" a
callback will be automatically added
to your Ajax request.
Alternatively you could make your ajax request to a server-side script which does the cross-domain call for you, then passes the data back to your script
To update the answer (I guess, mostly for my benefit when I come looking for this answer later on), if are loading XML or something else, you can always ask the user if he will allow us to read from another site with this code:
try {
if (netscape.security.PrivilegeManager.enablePrivilege)
netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
} catch (e) {
alert("Sorry, browser security settings won't let this program run.");
return;
}
(from the RESTful web services book) But, this only works in firefox, when the html file is loaded from local file. So, not that useful.
One more solution: if all you need is the headers, you can specify "HEAD" as the method and it won't trigger the security issue. For instance, if you just want to know if the web page exists.
var client = new XMLHttpRequest();
client.open("HEAD", my_url, false);
client.send(null);
if(client.readyState != 4 || client.status != 200) //if we failed
alert("can't open web page");
Some more details would be nice: which AJAX library are you using, what would you like to achive, how you do it.
For example it can be a cross-domain Ajax request, which is not allowed. In this case use JSON.
I came across this problem recently and it was while I as AJAX loading the local request, not cross site scripting problem. Also, Jimmy himself seems to have the same problem. This seems to be the FF security problem, this article describes the cause and the solution to access to restricted uri denied" code: "1012 problem.
Sorry, got that error using JQuery
$.ajax on FireFox 3. Tried jsonp
suggestion but I think that will only
work with something that will serve up
json. I'm trying to create a sample
local html file based mashup that will
pull data from Yahoo!Finance, but they
are serving .csv, so I think I'm SOL.
– Jimmy Chandra (Sep 9 at 17:20)
I hope you'll find it useful.