SyntaxError: expected expression, got '}' - javascript

I'm starting to program with JS and PHP and my question is why the console give this error, I search a lot of websites but without clues about this. If you can help I'll apreciate it
<script>
$(function(){
$("#btn_enviar").click(function(){
$.ajax({
type: "post",
url: "registro.php",
data: $("#form_members").serialize(),
success: function()
});
});
return false;
});
</script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

you forgot {} after function()
Updated JS
$(function () {
$("#btn_enviar").click(function () {
$.ajax({
type: "post",
url: "registro.php",
data: $("#form_members").serialize(),
success: function (){}
});
});
return false;
});

Related

Uncaught TypeError: $.ajax is not a function at another function

I could not fix ajax error on the site.
Error : Uncaught TypeError: $.ajax is not a function at Hei
My code are below.
Where am I making mistakes?
solutions in other posts did not help
jquery-3.3.1.js added removed
jquery-3.3.1.slim.min.js added removed
jquery.unobtrusive-ajax.min.js added removed
But the error continues
<script src="~/Scripts/jquery-3.3.1.js"></script>
<script src="~/Scripts/custom.fle_upload.js"></script>
<script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script>
<script type="text/javascript">
jQuery(document).ready(function ($) {
Hei();
});
function Hei() {
var tbl = $('#dvHei');
$.ajax({
async: false,
url: '/Hei/getHei',
contentType: 'application/html ; charset:utf-8',
type: 'POST',
dataType: 'html'
}).success(function (result) {
tbl.empty().append(result);
}).error(function () {
});
}
</script>
Welcome to the community. Sometimes, the issue could be the current scope for some reason. I've done the following without any issues, unless something was "repackaging" the "$" pointer to something else:
function Hei() {
var tbl = $('#dvHei');
$.ajax({
async: false,
url: '/Hei/getHei',
contentType: 'application/html ; charset:utf-8',
type: 'POST',
dataType: 'html'
}).success(function (result) {
tbl.empty().append(result);
}).error(function () {
});
}
//Same as document ready
$(function() {
Hei();
});
If that doesn't work you can try this to see if it recognizes tbl as a jQuery object, as a way of debugging the issue?
var tbl = $('#dvHei');
alert(tbl.length);
$.ajax({

ajax call function is not defined error

I'm using jquery's ajax function and am getting error:
'getQty is not defined'
Where is my mistake?
jQuery:
function getQty()
{
var dataString = "itemId=" +$(".itemId").val();
$.ajax({
type: "GET",
url: "getQty.php",
data: dataString,
success:function(data)
{
$(".qty").val(data);
}
});
}
HTML:
{$itemArray[sec].itemNm}
Best practice is
$('#myLink').click(function(){ getQty(); return false; });
You're calling the wrong function in the HTML:
onClick="getorder();"
should be:
onClick="getQty();"

simple PHP AJAX : How to call a php script

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>

Some Script Error , not able to find solution

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()},

jQuery Ajax Function

I am trying to write a jQuery Ajax function, to reduce the amount of code in the page because the script is called a few times in the page. I can't seem to get it to work. Here is the code i have:
var loadURL = $(this).attr('href');
function load(){
$.ajax({
url: loadURL,
type: 'GET',
cache: true,
data: {
delay: 4
},
success: function(data) {
$('#load').html(data)
}
});
return false;}
$('#one').click(function(){ load(); });
$('#two').click(function(){ load(); });
$('#three').click(function(){ load(); });
Two
Two
Two
can anyone guide me here please?
i think the way you are getting href is wrong
function load(item){
var loadURL = $(item).attr('href');
$.ajax({
url: loadURL,
type: 'GET',
cache: true,
data: {
delay: 4
},
success: function(data) {
$('#load').html(data)
}
});
return false;}
$('#one').click(function()
{
load($(this));
return false; //you should mention return false here
});
It is worth noting that you can define your own jQuery functions if you wish..

Categories