Uncaught TypeError: Illegal invocation ajax - javascript

This is my code:
$.ajax({
url:"../proc/insur.php",
method:"POST",
data:{carmodel:carmodel,usage:usage,mk:mk,sdate:startdate},
processData: false,
error: function() {
alert("fialed");
},
success: function(data){
$('#insurance').html(data);
}
});
I also tried the code below, but my values were not sent
processData: false
what can i do?

I think you need to pass strings. Use stringify for your data.
let data = JSON.stringify({carmodel:carmodel,usage:usage,mk:mk,sdate:startdate});

Related

I don't know why but my ajax POST method is not returning any response

This is my code , When I click on submit , somehow the data is inserting but that echo data in back php form is not showing in this front ajax js code , please tell me if anything is wrong in my data
var formData = new FormData(this);
$.ajax({
url: '../back/regback.php',
type: 'POST',
data: formData,
success: function (data) {
alert(data);
},
cache: false,
contentType: false,
processData: false
});
}
return false;
ok this is my full js code
$(document).ready(function(){
$('form#off').submit(function(event){
event.preventDefault();
if($('#name').val()==''){
$('#nameid').text('Plase Select Customer Name ');
return false;
}
else{
var formData = new FormData(this);
$.ajax({
url: '../back/regback.php',
type: 'POST',
data: formData,
success: function (data) {
//alert('data has been added');
error: (err)=>{console.warn(err)}
// location.href='gst_chargeoff.php';
alert(data);
},
cache: false,
contentType: false,
processData: false
});
}
return false;
});
});
The ajax call is working fine. It is also getting response from the url. If there would be any server side error , It can be detected in the error: of the ajax parameter.
In your code it was written incorrectly, the same i have corrected in the below code, you will get the error in console if there will be any server side error. else the response will be returned properly.
Check the below code.
$(document).ready(function(){
$('form#off').submit(function(event){
event.preventDefault();
if($('#name').val()==''){
$('#nameid').text('Plase Select Customer Name ');
return false;
}
else{
var formData = new FormData(this);
$.ajax({
url: '../back/regback.php',
type: 'POST',
data: formData,
success: function (data) {
//alert('data has been added');
// location.href='gst_chargeoff.php';
alert(data);
},
error: function(err){
console.log(err);
},
cache: false,
contentType: false,
processData: false
});
}
return false;
});
});
You forgot to add the error attribute to your AJAX request. It's most likely throwing an error.
error: (err) => {
console.warn(err)
}
Wrap the entire $.ajax block inside a console.log($.ajax({...}));.
Then look into the console for the response codes for more info
Also you can use this to find more about the case:
error: function(err){
console.log(err);
}

jquery ajax Uncaught SyntaxError: Unexpected token : while calling an api

I am trying to get a json response from the comicvine api but am getting the following error. comicvine.gamespot.com/:1 Uncaught SyntaxError: Unexpected token :
I see my json result, formatted, in the response body but am getting the console error above.
export function getSeriesFromComicVine() {
const url = "http://comicvine.gamespot.com/api/characters/?api_key=f18c6362ec6d4c0d7b6d550f36478c1cd6c04a49&filter=gender:male,name:hawkeye&format=json&callback=?";
$.ajax({
url: url,
// data: {test: "test"},
type: 'GET',
crossDomain: true,
jsonpCallback: 'callback',
dataType: 'jsonp',
jsonp: false,
jsonpCallback: "myJsonMethod"
success: function (data) {
console.log(data);
}
});
}
You need to set format=jsonp not json
the jsonp callback parameter name needs to be json_callback according to comicvine.gamespot.com - I found this out by going to url https://comicvine.gamespot.com/api/characters/?api_key=[your api key]&filter=gender:male,name:hawkeye&format=jsonp in the browser, and it told me what was missing - very friendly API - the response had an error value
"'jsonp' format requires a 'json_callback' argument"
and no need for callback=? in the url - seeing as jquery adds the callback parameter and it isn't named callback
function getSeriesFromComicVine() {
const url = "https://comicvine.gamespot.com/api/characters/?api_key=[your api key]&filter=gender:male,name:hawkeye&format=jsonp";
$.ajax({
url: url,
type: 'GET',
dataType: 'jsonp',
jsonp: "json_callback",
success: function (data) {
console.log(data);
}
});
}

Uncaught SyntaxError: Unexpected token when using jQuery Ajax in Codeigniter

I am getting the error "Uncaught SyntaxError: Unexpected token" when using jQuery Ajax in Codeigniter.
Here is my code:
function add_to_shopping_cart(base_url){
$.ajax(function(){
url: base_url+'cart/add_to_cart',
type: 'post',
data: $('#product_form').serialize(),
dataType: 'html',
success: function(html){
}
});
}
The error is on the line "type: 'post',"
I have done ajax functions thousands of times and can't see what's causing this thanks
That code makes no sense. Remove the function() part in the $.ajax call:
function add_to_shopping_cart(base_url){
$.ajax(/*no function() here*/{
url: base_url+'cart/add_to_cart',
type: 'post',
data: $('#product_form').serialize(),
dataType: 'html',
success: function(html){
}
});
}
What you pass to ajax should be an object initializer, not a function. If it were a function, the content would need to be function code rather than a set of property initializers.

Uncaught TypeError: Cannot read property 'ajax' of undefined When I'm trying to send data to url

I have created a form to get feedback from user, I’m simply trying to send form data to url, but I’m getting this error:
Uncaught TypeError: Cannot read property 'ajax' of undefined
function sendData(){
$.ajax({
url: "www.yashkjhsah.php",
type: "POST",
async: true,
data: $(".contacts_form").serialize(),
dataType: "html",
success: function(data)
{
alert(data);
if(data!="Error in Insert Query!")
{
alert("Thank you for Enquiry we will send answer in your Mail.");
}
else
{
alert("Error while saving the data");
}
}
});
}
The error message says that jQuery is not define.
You must include jQuery before doing anything with $.ajax
Put this line in the html page before your script :
<script src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
I had the same problem and I solved it, by changing the dollar symbol $ with jQuery.
jQuery.ajax({
type: "POST",
url: "...",
data: jQuery('myForm').serialize(),
success: function(msg) {
console.log('success!');
},
});
That means that jquery has not been loaded.
Make sure that you have the script in your html, and also wrap the call to this function sendData inside a
$(document).ready(function(){
//do stuff
})

Illegal Invocation while passing data attribute on Ajax

I am trying to get the data attribute of a button with id delete_btn and send it through ajax. As a result, I am getting the following error.
Uncaught TypeError: Illegal invocation
JQuery
var id = $( "#delete_btn").attr('data-identifier');
$.ajax({
dataType: "json",
url: apiURL,
data: { 'req': 'delete', 'id': id},
success: function(data){
// do something
},
error: function (textStatus){
//do something
}
});
I have even tried getting the attribute value by
$( "#delete_btn" ).data( "identifier")
But got the same error
Try to set processData: false in ajax settings like this
$.ajax({
url : base_url+'index.php',
type: 'POST',
dataType: 'json',
data: data,
cache : false,
processData: false
}).done(function(response) {
alert(response);
});
Try to add this four attribute to your ajax call:
async: false,
cache: false,
contentType: false,
processData: false,
Hope this help!

Categories

Resources