Errors when using ajax post request to geoserver - javascript

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.

Related

how to print return variable from api(javascript) inside php tag?

How do I print the success variable r in script tag. The r variable contains the value from api. But I am getting php errors.
if (Login::isLoggedIn()) {
echo "
$('.btn1').click(function(){
$.ajax({
type: 'POST',
url: 'api/follow?id={$userid}&artid={$id}',
processData: false,
contentType: 'json/application',
data: '',
success: function(r){
console.log(r)
('#followdiv').html("'+r.follow+'")//error
},
error: function(r){
}
});
});
";
}
how do I print r.follow?
I tried in different ways but its not working. Plz help me.
You don't need to concatenate r.follow with anything, so get rid of "'+r.follow+'".
Your contentType: 'json/application' is wrong in two ways: first, the correct name is application/json; second, you're sending empty data, that's not valid JSON. You're sending all the parameters in the URL, it's not clear why you're using POST at all.
If you meant to specify that the response is JSON, that's done using the dataType: option, not contentType:.
echo "
$('.btn1').click(function(){
$.ajax({
type: 'POST',
url: 'api/follow?id={$userid}&artid={$id}',
processData: false,
dataType: 'json',
data: '',
success: function(r){
console.log(r)
$('#followdiv').html(r.follow)
},
error: function(r){
}
});
});
";

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.

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!

AJAX - Cross-domain don't work

I was reading many things about that the json is great replacement for XMLHttpRequests. I tried it and it don't works:
$.ajax({
crossDomain: true,
url: settingsURL,
type: "POST",
dataType: 'JSONP',
parseAsHtml: true, cli: 'help',
success: function(data) {
data=$(data).find('div#TestDivContent');
$('#TestDivContent').append(data);
},
error: function() {
$('#TestDivContent').append("<p>Can't Connect</p>");
}
});
and im getting...
Uncaught SyntaxError: Unexpected token <
Please Check the code below that is working like a charm in Cross Domain ().
if You Have control of both the Domains i.e., Domain1.com & Domain2.com
//Ajax Script in Domain1.com
//No Conflict is the code snippet from my sample code You can delete it if not required no issues
<script type="text/javascript">jq1102 = jQuery.noConflict( true );</script>
<script type="text/javascript" >
function jsonp(n){
//GET Response is Here
alert(n);
}
jq1102(function(){
jq1102.ajax({
type: "GET",
dataType: "jsonp",
url: 'http://domain2.com/ClientSiteApi/',
crossDomain: true,
complete: function(data){
//Any Action You Like to Trigger After Complete
},
error: function(jqXHR, textStatus, ex) {
//Nothing to Change Here
}
});
})
</script>
Response from Domain2.com
echo 'jsonp("hello")'; //You Can place JSON string in replace of the Hello String

Categories

Resources