I don't have any expirience in ajax functions of jQuery. I'm trying to make simple call like:
$.get ("ajaxsupport/login");
I have a servlet with url-pattern ("ajaxsupport/login").
When I type in browser's address field "http://localhost:9090/ajaxsupport/login" I see some result. But $.get (..) doesn't even make a call.
What is the problem?
I use jquery 1.3.1
$.get
is an asynchronous method call by default, meaning the caller remains in control. That's why something must happen, when the request has been executed. You specify that by defining a callback.
jQuery.get( url, [data], [callback], [type] )
In your case (note the prepended '/', it may not be necessary, depending on the scripts location, though):
<script type="text/javascript" charset="utf-8">
$(document).ready(function(){
$.get("/ajaxsupport/login",
function(data, textStatus){
// just prompt the response and the status message
alert(data + "
" + textStatus);
}
);
});
</script>
Get a tool like fiddler. Watch at how the request goes over the wire. Is it going to where you think it's going? Are you getting a response? Can you put a breakpoint on your web service call?
Try to find out if you get a result:
$(document).ready(function(){
$.ajax
({
type: "GET",
url:"/ajaxsupport/login",
success: function(result)
{
alert("I'm a success");
}
});
});
You can also use firebug to what is requested and returned.
**Two Variable asynchronous sending to server [Raw JavaScript || Jquery ]] **
var demo = document.querySelector('#demo');
var x = 0;
var y = 0;
//pure
demo.innerHTML = x;
demo.addEventListener('click',()=> {
x=5;
demo.innerHTML = x;
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
console.log(xhttp.responseText);
}
};
xhttp.open("POST", 'http://localhost/target/index.php', true);
xhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhttp.send('data='+[x,y]);
});
//framework
$('#demo2').html(y);
$('#demo2').click(()=>{
y=5;
$.ajax({
type: 'post',
url: 'http://localhost/target/index.php',
data: {
'y': y,
'x' : x
},
success: function (response) {
console.log(response);
},
error: function () {
console.log("error");
}
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
#demo,#demo2{
height: 100px;
width: 100px;
background-color: #f12100;
}
#demo2{
background-color: #20c997;
}
</style>
</head>
<body>
<div id="demo"></div>
<br>
<div id="demo2"></div>
<script
src="https://code.jquery.com/jquery-3.4.1.min.js"
integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo="
crossorigin="anonymous"></script>
<script type="text/javascript" src="script.js"></script>
</body>
</html>
</div>