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.
Related
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});
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);
}
});
}
I am trying to send a http post request using ajax post to geoserver. I get the following error in chrome.
Uncaught SyntaxError: Unexpected identifier
when I use data: --my query-- and
Uncaught SyntaxError: Unexpected token ILLEGAL
when I use data: --my query--.
I see that it is the error is due to the closing tag eg: </ogc:PropertyName>
This my code:
$.ajax({
type: "POST",
url: "http://localhost/geoserver",
data: '
<wfs:GetFeature
xmlns:wfs="http://www.opengis.net/wfs"
xmlns:ogc="http://www.opengis.net/ogc"
service="WFS" version="1.1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.opengis.net/wfs http://schemas.opengis.net/wfs/1.0.0/WFS-transaction.xsd"
maxFeatures= "13" >
<wfs:Query typeName="*:MyFeatures_df16" xmlns:feature="http://www.openplans.org/topp">
<ogc:Filter>
<ogc:And>
<ogc:Or>
<ogc:PropertyIsEqualTo>
<ogc:PropertyName>ID</ogc:PropertyName>
<ogc:Literal>98400005701</ogc:Literal>
</ogc:PropertyIsEqualTo>
</ogc:Or>
</ogc:And>
</ogc:Filter>
</wfs:Query>
</wfs:GetFeature> ',
contentType: "text/xml",
dataType:"text",
crossDomain: true,
cache: false,
error: function() {alert('it doesnt work')},
success: function(result){ $("#div1").html(result);}
});
});
});
You are not naming your data that you are sending in your Ajax call and you are sending a string without a key. In your case I would either pass a String with a key or an Object.
A String: 'xml=data'
An Object: { xml: 'data'}
Source - http://api.jquery.com/jquery.ajax/
try -
data: { xml: encodeURIComponent('
<wfs:GetFeature
xmlns:wfs="http://www.opengis.net/wfs"
xmlns:ogc="http://www.opengis.net/ogc"
service="WFS" version="1.1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.opengis.net/wfs http://schemas.opengis.net/wfs/1.0.0/WFS-transaction.xsd"
maxFeatures= "13" >
<wfs:Query typeName="*:MyFeatures_df16" xmlns:feature="http://www.openplans.org/topp">
<ogc:Filter>
<ogc:And>
<ogc:Or>
<ogc:PropertyIsEqualTo>
<ogc:PropertyName>ID</ogc:PropertyName>
<ogc:Literal>98400005701</ogc:Literal>
</ogc:PropertyIsEqualTo>
</ogc:Or>
</ogc:And>
</ogc:Filter>
</wfs:Query>
</wfs:GetFeature> ')}
$.ajax({
type: "POST",
url: "http://localhost/geoserver",
data: 'http://schemas.opengis.net/wfs/1.0.0/WFS-transaction.xsd" maxFeatures= "13" > ID 98400005701',
contentType: "text/xml",
dataType:"text",
crossDomain: true,
cache: false,
error: function() {alert('it doesnt work');},
success: function(result){ $("#div1").html(result); }
});
If you actually format it properly, you can see you're missing some comma's and stuff. I don't know what you're trying to do with your data, but thats probably where it's going wrong.
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
})
I have a error when I tried to send post data with ajax() method.
I have a array with :
acpitool
aide
I use encodeURIComponent() for passing the array with data: but the ajax method fail.
Could you help me?
Thanks
Edit :
This is the ajax call
$.ajax({
url: 'AjaxSearch.php',
dataType: 'json',
data: param+"="+package,
type: 'POST',
success: function(data) {
}
});
package is a Array like this :
var package = new array("acpitool","aide");
Sometimes, I have this :
var package = new array("bonnie++");
For both, I have an error :
Uncaught Syntax error, unrecognized expression: +
$.ajax({
url: 'AjaxSearch.php',
dataType: 'json',
data: {
param: $.param(package);
}
type: 'POST',
success: function(data) {
}
});
use jQuerys .param() method to serialize an array.
Description: Create a serialized representation of an array or object, suitable for use in a URL query string or Ajax request.
update
based on your comment, try this
data: {
param: package.join(',');
}