I'm trying to get my javascript function to use ajax to call a php script. The php script will update a MYSQL table but I've tested the PHP script and it works fine.
This is my function:
function rate()
{
$.ajax({
data: '' ,
url: 'update.php',
method: 'GET',
success: function(msg) {
alert(msg);
}
});
}
and the function is called later with:
rate();
The php script doesn't need any information given to it, it just needs to be called, can anyone point out where I'm going wrong.
Just use like in this example:
<script>
function rate() {
$.get("update.php");
}
</script>
If you dont need to pass the data to the PHP page, you can omit 'Data' from the ajax parameters.
<script>
function rate(){
$.ajax({
url: 'update.php',
method: 'POST',
success: function(msg) {
alert(msg);
}
});
}
rate();
</script>
Have you included jquery library
Try this
<script src="http://code.jquery.com/jquery-latest.min.js"
type="text/javascript"></script>
<script>
function rate()
{
$.ajax({
data: '' ,
url: 'update.php',
method: 'GET',
success: function(msg) {
alert(msg);
}
});
}
rate();
</script>
Here is an example of how I use the ajax call:
$.ajax({
type: "POST",
url: "page_url",
dataType: 'json',
data: {
'date1' : date1,
'call': 'function_name'
},
beforeSend: function(){
$("#loading").show();
},
success : function(response){
},
complete: function(){
$("#loading").hide();
}
})
and on php part I add:
function function_name($request){
your code here
}
if (!empty($_POST['call'])) {
die($_POST['call']($_POST));
}
i show my all working code, try it:
<html>
<head>
<script type="text/javascript" src="/js/jquery-1.8.2.js"></script>
<script>
function rate() {
$.get("update.php");
}
</script>
</head>
<body>
<button onclick="rate()">Click me</button>
</body>
</html>
Related
I created self-signed certificate and bind with site for using https protocol and also its working fine in IIS but problem is that whenever I access contained webservice url(https://webservice/webmethod) into jquery ajax call for posting data and its cant work.
enter code here
<script src="jquery-1.9.1.js"></script>
<script type="text/javascript">
function btncallWebService() {
$.ajax({
//url is just for understanding
url: 'https://localhost/.../EditFormServices.asmx/WebSvcSave',
data: { sData:"bbbb" },
method: 'post',
dataType: 'xml',
success: function (respo) {
alert("success"+respo.d);
},
error: function (error) {
alert("error");
}
});
}
</script>
Here we go:
Your url is wrong
<script type="text/javascript">
function btncallWebService() {
$.ajax({
//url is just for understanding
url: '/WebSvcSave',
data: { sData:"bbbb" },
type: 'POST',
dataType: 'xml',
success: function (respo) {
alert("success"+respo.d);
},
error: function (error) {
alert("error");
}
});
}
</script>
Hope it helps;)
I'm trying to make a post request with Ajax but it doesn't work .
In the following code the first alert appears but the second ,inside the success function, doesn't appear.
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="jquery-2.1.4.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
testscore ="tryagaiDDDn"; //testvalue to enter into the mysql database
alert("xoa"+testscore);
$.ajax({
type: "POST",
url: "chatp.php",
data: { 'tryagain':tryagain },
success : function(data){
alert(data);
}
});
});
});
</script>
</head>
</html>
Thanks
tryagain is not defined -
$(document).ready(function () {
$("#button").click(function () {
testscore = "tryagaiDDDn";
alert("xoa" + testscore);
var tryagain = "your value"; //tryagain defined here
$.ajax({
type: "POST",
url: "chatp.php",
data: { 'tryagain': tryagain },
success: function (data) {
alert(data);
}
});
alert("hello");
});
});
I already defined a handler for "get_list" but whenever I run this AJAX code, the terminal does not show that I have called this "get_list" function. Please help.
<script>
function update_list() {
$.ajax({
url: "/get_list/",
type: "POST",
data: {'csrfmiddlewaretoken': $.cookie("csrftoken"), 'type': 'open'},
success: function(data) {
alert("PUSHED")
$('.ticketList').html(data);
}
});
}
$(document).ready(function() {
update_list();
});
</script>
I am trying to parse some JSON data through AJAX i followed an example from here:
how to parse json data with jquery / javascript?
On the example, they can get it working, but on my exmaple, the turned blank.
I tried just echoing the php, the JSON displayed with no problem either. Wondering what the problem is.
<!DOCTYPE HTML>
<html>
<head>
<link type ="text/css" rel="stylesheet" href= "css/bootstrap.css">
<link type ="text/css" rel="stylesheet" href= "css/account.css">
</head>
<body>
<p id="result">fefe</p>>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script>
$.ajax({
type: 'GET',
url: 'get.php',
data: { get_param: 'value' },
dataType:'json',
success: function (data) {
var names = data
$('#result').html(data);
}
});
</script>
</body>
</html>
What the JSON result looks like in php:
[{"id":"1","userid":"26","title":"654","description":"654"}]
what is the type of data?
try add this line in your code on success function
success: function (data) {
console.log(data);
}
is it an array of objects, if yes maybe you can try this
$.ajax({
type: 'GET',
url: 'get.php',
data: { get_param: 'value' },
dataType:'json',
success: function (data) {
for(var i=0; i<data.length; i++) {
// do your things here using data[i].description until data[i].userid
}
}
});
Try this
$.ajax({
type: "GET",
dataType: "json",
url: "getjobs.php",
data: data,
success: function(data) {
$('#result').html(data);
}
});
Try setting $.ajax's datatype is set to jsonp. Also try to alert the return value.
$.ajax({
type: 'GET',
url: 'get.php',
data: { get_param: 'value' },
dataType:'jsonp',
success: function (data) {
alert('data is ::' +data);
$('#result').html(data);
}
});
Try this:
$.ajax({
type: 'GET',
url: 'get.php',
data: { get_param: 'value' },
dataType:'json',
success: function (data) {
console.log(data);
}
});
in the console(such as chrome browser`s development tools), you can see the actual result.
i want to get the 'printed value' of html pages.
i tried below query, but showGetResult() just return 'null value'
but my apache server logs printed i accessed index.php when i try this code.
(index.php just print helloworld)
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"><\script>
<script type="text/javascript">
function showGetResult( name )
{
var result = null;
jQuery.ajax({
url: 'http://localhost/index.php',
type: 'get',
dataType: 'text/html',
success:function(data)
{
alert(data);
result = data;
}
});
return result;
}
document.write(showGetResult('test'));
</script>
This is the way AJAX works (asynchronously, like the name suggests). The showGetResult function returns before the AJAX call completes. showGetResult will therefore simply return null since that's what you've assigned to result.
Move any code that depends on the result of the AJAX call inside the success callback. Alternatively, you could make the call synchronous, but that's not usually what you want.
I think what you want to do is this.
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"><\script>
<script type="text/javascript">
function showGetResult( name )
{
jQuery.ajax({
url: 'http://localhost/index.php',
type: 'get',
dataType: 'text/html',
success:function(data)
{
alert(data);
document.write(data);
}
});
}
showGetResult('test');
</script>
AJAX requests are aynchronous by default; you can't return their result in a function, you need to use callbacks. The easiest way to achieve what you want is to put your code that handles your data in your success handler:
success:function(data)
{
alert(data);
result = data;
document.write(showGetResult('test'));
}
Also, don't use document.write.
You have the wrong dataType per the documentation for jQuery.ajax:
"html": Returns HTML as plain text; included script tags are evaluated when inserted in the DOM.
So you want to use html:
...
dataType: 'html',
...
In addition, as others have said, the ajax request is asynchronous. So you need to restructure your code. For example:
function showGetResult( name )
{
var result = null;
jQuery.ajax({
url: 'http://localhost/index.php',
type: 'get',
dataType: 'html',
success:function(data)
{
alert(data);
result = data;
document.write(result);
}
});
}
showGetResult('test');
You're missing a fundamental point here. The success method is not run when you call showGetResult. It is run asynchronously.
At the point that you put return result; it is still null (because success has not yet been invoked).
What you need to do is have document.write execute after success is invoked. Either like this:
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"><\script>
<script type="text/javascript">
function showGetResult( name )
{
var result = null;
jQuery.ajax({
url: 'http://localhost/index.php',
type: 'get',
dataType: 'text/html',
success:function(data)
{
alert(data);
document.write(data);
}
});
return result;
}
//document.write(showGetResult('test'));
showGetResult('test');
</script>
Or with a callback:
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"><\script>
<script type="text/javascript">
function showGetResult( name )
{
var result = null;
jQuery.ajax({
url: 'http://localhost/index.php',
type: 'get',
dataType: 'text/html',
success:function(data)
{
alert(data);
writeToDocument(data);
}
});
}
function writeToDocument(data) {
document.write(data);
}
showGetResult('test');
</script>
Rather than using document.write on what you expect the function to return, the success callback can take care of that for you, like so:
success:function(data) {
document.write(data);
}
jQuery.ajax({
async: false, //add async false
url: 'http://localhost/index.php',
type: 'get',
dataType: 'text/html',
success:function(data)
{
alert(data);
result = data;
}
});