I have java URL which includes php page header .Now on header there is button which calls javascript function which eventually calls php file (ajax call)
the url is like this
http://localhost:8080/project/welcome.htm
header is header.php
body is javas body and again
footer is php footer.
How can I call my php page.
Please suggest
thanks
You can do this using jquery. It is the easy one
Include jquery through following link
http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js
and call following function in Javascript
$.ajax({
url: "header.php",
data: { name: "John", location: "Boston" }
})
.done(function( msg ) {
alert( "Data Saved: " + msg );
});
in .done function, you can do anything you want to do after calling your PHP file
Related
Is there a way to send ajax requests in a Java program? I was thinking that there might be a jquery library somewhere, but I don't even know if jquery would be something that I should use. I'm trying to get the data from a certain request from a website as shown in this function:
function searchFraze(fraze, page) {
page = typeof page !== 'undefined' ? page : 1;
$.ajax({
url: 'ajax/tradeCsRight.php',
type: 'POST',
data: "search=1&type=10&fraze="+fraze+"&page="+page,
success: function(data) {
$("#itemlist").html(data);
}
});
}
Basically, I want to POST custom data in the data field above to the website (http://csgolounge.com/). Honestly, I don't even know if I should be doing what I'm doing this way, or if I should use some other method.
Also, in FireBug, I can see the contents of the tradeCsRight.php file as seen here (which is my goal, to see the html content of this): http://i.imgur.com/8ACnGbp.png
If I open the actual file in chrome, however, the page and html is blank (http://i.imgur.com/LHtKyUb.png). Can someone tell me why this is?
I am trying to make a login page in java J2EE. Actually, I succed in making a jsp page, called login.jsp, an ActionSupport class called LoginAction. In my jsp I've created some text area, password area and submit, allowing me to send informations entered in those areas and get them in my action. This works fine.
My problem is that I don't succed in using another function than execute. In my action I have several other functions and I can't call them in my ajax function :
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
<script>
$(function() {
$('#submit').click(function() {
var login=$("#login").val();
var password=$("#password").val();
$.ajax({
type: 'POST',
url: 'login.action/test', // -> here it fails.............
data: { login: login, password: password },
success: function() {
alert('YEAH'); },
error: function() {
alert('Request failed'); }
});
});
});
</script>
What should I put in url area, if I want for example to call test function in my LoginAction class? (I've done a test.jsp page, it doesn't change anything...)
OK I found the answer. So here it is :
It's called "dynamic method invocation" (with wildcard method), and you can find the doc here :
http://struts.apache.org/release/2.0.x/docs/action-configuration.html#ActionConfiguration-WildcardMethod
It is very important to use last struts2 release to avoid critical security bugs while using wildcard method - see https://struts.apache.org/release/2.3.x/docs/s2-015.html for details.
use "function nametest()" instead of "function()"
I am having a problem. I have a form submit that uses jQuery JSONP to make a call BACK to my servlet. This code is embedded in another domain, hence the usage.
Here is my form submit AJAX:
<script src='http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js' type='text/javascript'></script>
<script language='Javascript' type='text/javascript'>
$(\"#form1\").submit(function(event) {
event.preventDefault();
var $form = $(this),
choice2 = $form.find( 'input[name=\"personChoice\"]' ).val(),
url = $form.attr( 'action' ),
$.ajax({
data: {choice:choice2},
url: url,
dataType: 'jsonp',
jsonp: 'callback',
jsonpCallback: 'updatePage'
});
});
function updatePage(renderHTML) {
alert(renderHTML);
}
</script>
I can confirm in my Java servlet that the request.getParameter("callback") is populated with updatePage. What I need to do is send back a response in JSONP format within the Java Servlet. How does one do that? I have something really simple like this at the moment:
Java Servlet code:
response.setContentType("text/javascript");
PrintWriter out = response.getWriter();
String renderHTML = "{ renderHTML = 'Successful'}";
out.println(renderHTML);
I also tried JSON content type response, the alert in my javascript updatePage doesn't get called.
What is the trick with Java servlet's and response back on a jsonpCallback???
A JSONP response must be wrapped inside a function call which is specified in request with callback.
Something like
callback({ renderHTML = 'Successful'});
This should usually be taken care at a filter.
The jsonpCallback property is not what you think it is. It's a string (or a function returning a string) which jquery will use as part of the request URL. You can set it to false if you don't need that functionality. See the docs
You should use the 'success' property to set the response handler.
Something like this:
$.ajax({
data: {choice:choice2},
url: url,
dataType: 'jsonp',
jsonp: 'callback',
jsonpCallback: false,
error: function(xhr, status, error) {
alert("error");
},
success: updatePage
});
Also, the best content-type to use for JSONP is application/javascript.
Hope that helps.
I'm working with a third party that is generating div content based on a post response from my server (java servlet). One problem I have is that we have a list of radio buttons leveled in a form.
When I hit submit on that form, I need to make a post call to my server and re-render that div in the third party site. I have used different variations of jQuery to no avail.
I've included the most recent jQuery (also tried a sub 1.4 release of jQuery). When I hit the submit button on my form, I just render the same page and I do NOT render a call to the server.
How can I do this, update a div on the local page that renders my post results based on a form I write? Below is what I currently have:
Form:
<form action='\' id=\"form1\">... radio buttons ... </form>
<input hidden field name = value passed from Java method>
<input hidden field id = value passed from Java method>
<input hidden field the value of the selected checkbox>
HTML:
<script language='Javascript' type='text/javascript'>
$(\"#form1\").submit(function(event) {
event.preventDefault();
var $form = $( this ),
name2= $form.find( 'input[name=\"name\"]' ).val(),
id2= $form.find( 'input[name=\"id\"]' ).val(),
url = $form.attr( 'action' );
$.post( url, { name2:name, id2:id },
function( data ) {
var content = $( data ).find( '#content' );
$( \"#this_div\" ).empty().append( content );
});
});
</script>
Sending AJAX Requests from a Third Party site to your server:
Due to browser security requirements, it is not currently possible to make cross-domain AJAX requests to a third-party server. This means that the $.post request is limited to what is referred to as the same-domain policy.
Thus, if your server is example.com and the third party server is domain.com, domain.com cannot make AJAX requests to your server.
However, there is a technique you can use to circumvent this browser security. While it's not possible for XMLHttpRequests to be made cross domain, JavaScript <script> tag blocks can load JavaScript from any domain.
Script tag remoting, or JSONP, involves using a script tag to send a request to your server:
Script tag:
// from domain.com to your server, example.com, make a request using a script tag
var urlWithParams = "http://example.com/getHTMLForm.do?id2=" + id2 + "&name2" + name2;
var script = document.createElement("script");
script.setAttribute("type","text/javascript");
script.setAttribute("src", urlWithParams);
// create a script tag, which invokes your servlet
document.getElementsByTagName("head")[0].appendChild(script);
getHTMLForm.do is a hypothetical servlet that you're currently using to post the data and get HTML in the response. Instead of passing the parameters in the Request body using POST, you'll pass the data as query parameters.
Server response::
The server then responds with JSON that you generate on the server, but it's wrapped -- or padded -- inside a JavaScript function that is defined on the web page making the request.
// your response from your server
insertFormOnPage({"html":"<form action='#'><input name='name' /><input name='id' /></form>", "elem" : "#content"});
Third party Client side code:
For this technique to work, the third party site must have a function defined that matches the one your server will return:
function insertFormOnPage( data ) {
alert( data.html ); // prints the HTML for debugging
alert( data.elem ); // prints the selector you want to insert into
// inject the HTML into the #content DIV
$( data.elem ).html( data.html );
}
HTML on the third party site:
<!-- Let's just assume the third party site's DIV is empty for simplicity -->
<div id="#content"></div
Explanation:
Your server returns pure JavaScript to the client side, as JavaScript, as a function that executes immediately.
The function receives the following items as properties in a JavaScript object: The HTML, and the div id.
The function accesses the object's html and elem properties to access both the html string and the selector.
Using jQuery, the function injects the HTML inside the DIV#content element.
The last and final thing you should know about this technique is that it only supports GET methods, since that is how JavaScript is fetched from the server. This means that you'll need to make sure your server is configured to accept GET requests for this data and not POST requests.
JSONP Using jQuery:
While the above solution helps describe the concepts of what is happening under the hood, you may also want to check out jQuery getJSON. Specifically, look at the JSONP examples, which are the only way to make cross-domain requests without reloading the page.
$.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?jsoncallback=?",
{
tags: "cat",
tagmode: "any",
format: "json"
},
function(data) {
$.each(data.items, function(i,item){
$("<img/>").attr("src", item.media.m).appendTo("#images");
if ( i == 3 ) return false;
});
});
I am using Dojo library. How can I replace all the content of my jsp/html content. I am trying to dynamically reload my page when a data is updated.
Here is my dojo code:
function reloadPage() {
var thisUrl = '/CBS/a/customer/' + customerId + '/profile';
dojo.xhrGet({
url: thisUrl,
load: function (data) {
document.body.innerHTML = data;
},
error: function (data, ioArgs){
document.body.innerHTML = "unknown error";
}
});
}
The server returns a complete html code including the html tags. The data variable holds all the html tags. In my code I did document.body.innerHTML = data;which is wrong because the content of body is replaced by a whole html page. It looks like ajax is working because its updated dynamically but my buttons are not working anymore. Please help.
Why do you use AJAX if you want to update the whole page? Purpose of using of AJAX - update part of content(page), not updating the whole page. If you want to update the whole page, may be it will be more appropriate to use reload?