i have an unexpected token :
but i dont know why this is.
the code where it is happening.
<script type="text/javascript">
$('.delete-btn').click(function() {
$.ajax(function() {
type: 'POST',
url: 'ajax.php',
data: { filename: filename },
success: function(return) {
if(return == 'SUCCESS') {
$this = $(this).closest('tr');
$this.remove();
}
}
});
});
</script>
I hope someone can find why i get the unexpeded token : at the url: 'ajax.php', rule.
The syntax error is in your success callback. You've named the argument return, which is a reserved word. Call it something else.
Niet here, merging my answer into this one to complete the picture:
Change:
$.ajax(function() {
To:
$.ajax({
Pay closer attention to what you're writing :p
return is a reserved word in JavaScript. Use any other name other than return. I have used data instead of return. Also there is type is an error in ajax function you have written. updated the same. Use the below code
<script type="text/javascript">
$('.delete-btn').click(function() {
$.ajax({
type: 'POST',
url: 'ajax.php',
data: { filename: filename },
success: function(data) {
if(data == 'SUCCESS') {
$this = $(this).closest('tr');
$this.remove();
}
}
});
});
</script>
return is a keyword at javascript dont use it as a variable name
<script type="text/javascript">
$('.delete-btn').click(function() {
$.ajax(function() {
type: 'POST',
url: 'ajax.php',
data: { filename: filename },
success: function(data) {
if(data== 'SUCCESS') {
$this = $(this).closest('tr');
$this.remove();
}
}
});
});
</script>
Related
I am not able to pass value using ajax in php file.
Corrected Code
<script>
$("body").on('change', '#area', function () {
//get the selected value
var selectedValue = $(this).val();
//make the ajax call
$.ajax({
url: 'box.php',
type: 'POST',
data: {option: selectedValue},
success: function () {
console.log("Data sent!");
}
});
});
</script>
here the php code
<?php $val=$_POST['option'];echo $val; ?>
There are a few problems here:
It should be url, not rl. Also, you have type: POST' with it ending in a ', but no starting '.
It should be type: 'POST'.
It should then look like this:
$("body").on('change', '#area', function() {
var selectedValue = this.value;
$.ajax({
url: 'box.php',
type: 'POST',
data: {
option : selectedValue
},
success: function() {
console.log("Data sent!");
}
});
});
If you want to view your data on the same page after (as on box.php, you are echo'ing the value.), you can do this:
success: function(data) {
console.log(data);
}
This will then write in the console what option is, which is the value of #area.
Try the following code
$("body").on('change',function(){
$.ajax({
URL:<you absolute url>,
TYPE:POST,
Data:"variable="+$("#area").val(),
Success:function(msg){
<do something>
}
});
});
Hope this will help you in solving your problem.
Your just miss ajax method parameter spelling of 'url' and single quote before value of type i.e. 'POST'. It should be like
$.ajax({
url: 'box.php',
type: 'POST',
data: {option : selectedValue},
success: function() { console.log("Data sent!");}
});
I keep on getting this error on chromium but not on Firefox n its driving me nuts because I cant find a solution despite searching for hours now. I basically get JSON from the server and then wanna insert it into DOM. Here's my code...
function lookup(inputString){
if(inputString.length == 0){ //hide suggestions
$('#suggestions').empty()
.fadeOut();
}
else{ //make an AJAX call
$.ajax({
type: 'GET',
url: '{% url "search.views.search" inputString="xyz" %}'.replace("xyz", inputString.toString()),
dataType: 'json',
success: function(search_results){
suggestions = JSON.parse(JSON.stringify(search_results));
alert(suggestions[0].name);
}
})
}
}
You need a ; at the end of this:
$.ajax({
type: 'GET',
url: '{% url "search.views.search" inputString="xyz" %}'.replace("xyz", inputString.toString()),
dataType: 'json',
success: function(search_results){
suggestions = JSON.parse(JSON.stringify(search_results));
alert(suggestions[0].name);
}
});
same conclusion :)
you missed a semicolon ;
jslint could help you to find this kind of errors : http://www.jslint.com/
function lookup(inputString) {
if (inputString.length === 0) {
$('#suggestions').empty().fadeOut();
}
else {
$.ajax({
type: 'GET',
url: '{% url "search.views.search" inputString="xyz" %}'.replace("xyz", inputString.toString()),
dataType: 'json',
success: function(search_results) {
suggestions = JSON.parse(JSON.stringify(search_results));
alert(suggestions[0].name);
}
});
}
}
I have a little function, im trying pass 2 parameters for her, but dont works...
Any idea/sugestion?
Don't have problems with ajax, i have tested this code without parameters, putting direct on the function, but calling her, not works, sorry about the terrible english!!
function myfunction(var_data, var_field)
{
$(function()
{
$.ajax
({
url : "myscriptajax.php",
type: "POST",
data: var_data + $(this).val(),
dataType:"json",
success: function(data)
{
if(data.status)
{
$(var_field).val(data.somevar);
}
}
})
})
}
$("#medicocrm").change
(function()
{
myfunction("crm=","#mediconome");
})
// edited after here for best explanation about.
That works:
$(function()
{
$("#medicocrm").change
(function()
{
$.ajax
({
url : "abertura.ajax.php",
type: "POST",
data: "crm=" + $(this).val(),
dataType:"json",
success: function(data)
{
if(data.status)
{
$("#mediconome").val(data.nome);
}
}
})
return false;
})
$("#participantematricula").change
(function()
{
$.ajax
({
url : "abertura.ajax.php",
type: "POST",
data: "matricula=" + $(this).val(),
dataType:"json",
success: function(data)
{
if(data.status)
{
$("#participantenome").val(data.nome);
}
}
})
return false;
})
\i tried this with first answer...
and that not works:
function verifica(dados,campoid,camponome){
$.ajax({
url : "abertura.ajax.php",
type: "POST",
data: dados + campoid,
dataType:"json",
success: function(data){
if(data.status){
$(camponome).val(data.nome);
}
}
});
return false;
};
$("#medicocrm").change(function(){
verifica("crm=",this.value,"#mediconome");
});
$("#participante_id").change(function(){
verifica("id=",this.value,"#participante_nome");
});
Just do a revamp.
function myfunction(var_data, var_field, elementValue){
$.ajax({
url : "myscriptajax.php",
type: "POST",
data: var_data + elementValue,
dataType:"json",
success: function(data){
if(data.status){
$(var_field).val(data.somevar);
}
}
});
};
$("#medicocrm").change(function() {
myfunction("crm=","#mediconome", this.value);
});
Here we removed the DOMContentLoaded listener and passed the value of the element through to the function..
You can use $(this).val(); in place of this.value whatever floats your boat.
You have a whole lot of wrappers going on, and your use of $(this) is likely breaking it. Something like this should work:
function myfunction(var_data,$var_field,whatever_this_is_val){
$.ajax({
url : "myscriptajax.php",
type: "POST",
data: var_data + whatever_this_is_val,
dataType:"json"
}).done(function(data){
if(data.status){
$var_field.value = data.somevar;
}
});
}
$("#medicocrm").on('change',function(){
myfunction("crm=",this,document.getElementById(whatever_this_is).value);
});
Changes:
Unnecessary wrappers removed
Passing of $(this) ... you need to specifiy it
Cleanup syntax to modern use of .done().
Using vanilla JS where easily applied
You should also consider explicitly declaring the page that it calls to be JSON, rather than saying dataType:'json' in your call. Its bulletproof this way, and less work performed on all sides.
EDIT
If you are really just passing the value of the item changed, easiest way to do it:
function myfunction(var_data,$var_field){
$.ajax({
url : "myscriptajax.php",
type: "POST",
data: var_data + $var_field.value,
dataType:"json"
}).done(function(data){
if(data.status){
$var_field.value = data.somevar;
}
});
}
$("#medicocrm").on('change',function(){
myfunction("crm=",this);
});
That way worked!!!!!! With many wrappers, but... Worked!
callajax = (function(origem,dados,campo)
{$.ajax({
url : "abertura.ajax.php",
type: "POST",
data: origem + "=" + dados,
dataType:"json",
success: function(data){
if(data.status){
$(campo).val(data.nome);
}
else{
$(campo).val("");
alert('Não encontrado');
}
}
})
});
$(function(){$("#medicocrm").change
(function(){
callajax('crm',this.value,"#mediconome");
});
});
$(function(){$("#participantematricula").change
(function(){
callajax('matricula',this.value,"#participantenome");
});
});
$(function(){$("#prestadorcodsoc").change
(function(){
callajax('codsoc',this.value,"#prestadornome")
});
});
function LoadAdvListPage() {
$.ajax({
type: 'post',
url: <%= generatecharts %> ,
data: {
},
success: function (data) {
alert("success");
}
});
}
$(document).ready(function () {
LoadAdvListPage();
});
Above is my java script function which calls the Ajax method but don't know some how its throwing error like missing } after property list
can anybody guide me whts the problem?
I don't know if something is above code you posted but here is an extra }
$.ajax({
type : 'post',
url : <%=generatecharts%>,
data : {
},
success : function(data) {
alert("success");
}
});
} <------ this one you don't need if you don't have opening { somewhere up
Edit:
So there is opening { above. Try doing like #Yuriy Rozhovetskiy wrote in comment, it's url param so for sure it shouldn't be json, but "" are still needed .
you should try thisL
$(document).ready(function(){
LoadAdvListPage();
function LoadAdvListPage() {
$.ajax({
type : 'post',
url : <%=generatecharts%>,
data : {
},
success : function(data) {
alert("success");
}
});
};
});
Can't seem to get the variable getID to work. I'm trying to change the html of the div. I know that the variable has the right value.
$('.cardid').change(function() {
var getID = $(this).attr('value');
$.ajax({
type: "POST",
url: "inc/change_thumbnail.php",
data: "id="+getID,
cache: false,
success: function(data) {
$("#"+getID).html(data);
alert("success");
},
error: function (err) {
alert("error");
}
});
});
Write data in $.ajax as data: {id : getID}, instead of data: "id="+getID,
Use val to get the value of an input :
var getID = $(this).val();
As you're making a POST request, you should also use the data argument to let jQuery properly send the value :
$.ajax({
type: "POST",
url: "inc/change_thumbnail.php",
data: {id:getID},
cache: false,
success: function(data) {
$("#"+getID).html(data);
alert("success");
},
error: function (err) {
alert("error");
}
});
You can try this:
$('[id="'+getID+'"]').html(data);
and yes you should pass it this way:
data:{id:getID}