I am trying to send a post param. to request.php but it returns that the post param. are empty.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
$.ajax({
url: "request.php",
type: "POST",
data: "{key:'123', action:'getorders'}",
contentType: "multipart/form-data",
complete: alert("complete"),
success: function(data) {
alert(data);
},
error: alert("error")
});
remove " " from this data as data:{key:'123', action:'getorders'}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script>
$.ajax({
url:"request.php",
type:"POST",
data:{key:'123', action:'getorders'},
contentType:"multipart/form-data",
complete:alert("complete"),
success:function(data) {
alert(data);
},
error:alert("error")
});
</script>
You must use FormData for multipart/form-data ,and also need additional option in ajax ..
var request = new FormData();
request.append('key',123);
request.append('action','getorders');
$.ajax({
url: "request.php",
type: "POST",
data: request,
processData : false,
contentType: false,
success: function(data) {
alert(data);
}
});
This will help you. You don't want a string, you really want a JS map of key value pairs.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script>
$.ajax({
url:"request.php",
type:"POST",
data:{key:'123', action:'getorders'},
contentType:"multipart/form-data",
complete:alert("complete"),
success:function(data) {
alert(data);
},
error:function(){
alert("error");
});
</script>
This should work like a champ ,
construct object as below and stringify it as JSON.stringify(newObject) then there will be no chance of error
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script>
var newObject= new Object();
newObject.key= '123';
newObject.action='getorders'
$.ajax({
url:"request.php",
type:"POST",
data:JSON.stringify(newObject),
contentType:"multipart/form-data",
complete:alert("complete"),
success:function(data) {
alert(data);
},
error:function(){
alert("error");
});
</script>
Try this:
data: JSON.stringify({key: '123', action: 'getorders'}),
contentType: "application/json"
Related
all Ajax not working in my project,
my structure project is
http://localhost/aaa/beranda.php?h=Add-Pengeluaran
The jquery code is as follows
<script type="text/javascript">
$(document).ready(function() {
$('#tampil').load("tampil.php");
$("#Submit").click(function() {
var data = $('#form').serialize();
$.ajax({
type: 'POST',
url: "insert.php",
data: data,
cache: false,
success: function(data) {
$('#tampil').load("tampil.php");
}
});
});
});
</script>
please help me
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.
in my application I have to make an ajax call to php file.it works proper in all devices. but when I tried it on ipad mini it not calls the php, so that the functionality not works, I've seen so many question about this problem and edited my code like this.
jQuery.ajax({
type: "POST",
async: true,
cache: false,
url: "directory/phpfile.php",
data: data,
success: function(response) {
}
});
my old code is
jQuery.ajax({
type: "POST",
url: "wp-admin/admin-ajax.php",
data: data,
success: function(response) {
}
});
and the problem still cant resolve . so please any one tell me how to resolve this.
Please use this code
$("#ajaxform").submit(function(e)
{
var postData = $(this).serializeArray();
var formURL = $(this).attr("action");
$.ajax(
{
url : formURL,
type: "POST",
data : postData,
success:function(data, textStatus, jqXHR)
{
//data: return data from server
},
error: function(jqXHR, textStatus, errorThrown)
{
//if fails
}
});
e.preventDefault(); //STOP default action
e.unbind(); //unbind. to stop multiple form submit.
});
$("#ajaxform").submit(); //Submit the FORM
<script type='text/javascript'>
$(document).ready(function startAjax() {
$.ajax({
type: "POST",
url: "test.php",
data: "name=name&location=location",
success: function(msg){
alert( "Data Saved: " + msg );
}
});
});
My query to the database is with AJAX , when you click on a link the html returns me a list from the database.
My javascript code:
$(document).ready(function() {
$('.gnr a').on('click', function(){
var title = $(this).attr('title');
$.ajax({
async:true,
type: "POST",
dataType: "json",
contentType: "application/x-www-form-urlencoded",
url:"recibe.php",
data:"titulo="+title,
beforeSend:inicioEnvio,
success:llegadaDatos,
timeout:4000,
error:problemas
});
});
});
function inicioEnvio()
{
$(".lista-usuario").html('<img src="loading.gif">');
}
function llegadaDatos(respuesta) {
$(".lista-usuario").html(respuesta.datos1);
$(".lista_usuario").html(respuesta.datos2);
$("#art-list").html(respuesta.datos3);
}
function problemas(){$(".lista-usuario").text('Problemas en el servidor.');}
I have a problem with function
html(respuesta.datos1) and html(respuesta.datos2) apparently there conflicto. Some times "data1" = null other times "data2" =null. Please how could correct that problem?
How about:
function llegadaDatos(respuesta) {
var usuario = resupesta.datos1 || respuesta.datos2;
if (usuario) {
$(".lista-usuario").html(usuario);
}
$("#art-list").html(respuesta.datos3);
}
you need to pass the response as argument in you successhandler
$.ajax({
async:true,
type: "POST",
dataType: "json",
contentType: "application/x-www-form-urlencoded",
url:"recibe.php",
data:"titulo="+title,
beforeSend:inicioEnvio,
success:llegadaDatos(response), //<< !-important, your function expects an argument you didnt pass
timeout:4000,
error:problemas
});
});
try this:
$.ajax({
async:false,
type: "POST",
dataType: "json",
contentType: "application/x-www-form-urlencoded",
url:"recibe.php",
data:"titulo="+title,
beforeSend:inicioEnvio,
success:llegadaDatos,
timeout:4000,
error:problemas
});
The "async:false" may solve your problem here.
if(typeof respuesta.datos1 != undefined){
$(".lista-usuario").html(respuesta.datos1);
}
I bounded data to dropdownlist but when i
click on button i want value of selected item but i cant get.and also its going refresh.
<script type="text/javascript">
$(document).ready(function () {
$.ajax({
type: "POST",
async: false,
contentType: "application/json; charset=utf-8",
data: "{}",
url: "Drpodownlistbindingjquery.aspx/getdata",
dataType: "json",
success: ajaxSucceess,
error: ajaxError
});
function ajaxSucceess(response){
$.each(response.d, function (key, value) {
$("#ddlCategory").append($("<option></option>").val(value.Sname).html(value.Sno));
});
}
function ajaxError(response){
alert(response.status + ' ' + response.statusText);
}
});
</script>
And ,my second problem is
<script type="text/javascript">
$(document).ready(function() {
$("#btnsubmit").click(function(){
$.ajax({
type: "get",
url: "loginform.aspx/getdataval",
data:'{"uname":"'+$("#TextBox1").val()+'","passwod":"'+$("#TextBox2").val()+'"}',
contentType: "application/json;charset=utf-8",
dataType: "json",
sucess:function(data){
var Emp=data.d;
alert('welcome');
$("#output").append('<p>'+Emp.Sname+ ' ' + Emp.Sno+'</p>');
//here i want to give redirect link
},
error: function(e) {
alert(e);
}
});
});
});
</script>
i m comparing username and password but its giving error. anything is wrong here?
Drpodownlistbindingjquery.aspx is not a typo mistake am sure.
Thanks in advance.
Second problem is easy:
sucess:function(data){
var Emp=data.d;
sucess should be success.
And as #nbrooks pointed out:
url: "Drpodownlistbindingjquery.aspx/getdata"
Drpodown should be Dropdown.
Just try this one:
data:{"uname":$("#TextBox1").val(),"password":$("#TextBox2").val()},