Consider the following code:
var jsonData1 = $.ajax({
url: 'http://'+domainName+':9898/jolokia/read/*',
dataType:"json",
crossDomain: true,
beforeSend: function(xhr){
var username= '*';
var password= '*';
xhr.setRequestHeader("Authorization",
"Basic " + btoa(username + ":" + password));
},
async: false
}).responseText;
var parsed1 = JSON.parse(jsonData1);
Here when I directly access the url, it asks for a username and password, which when given shows the value.
But when I do it through an ajax call, it throws this error:
Failed to load resource: the server responded with a status of 401 (Unauthorized)
jquery.min.js:5 XMLHttpRequest cannot load http://10.91.240.21:9898/jolokia/read/* No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access. The response had HTTP status code 401.
(index):1 Uncaught SyntaxError: Unexpected token u
TRY 2:
var jsonData2 = $.ajax({
type:'GET',
url: 'http://'+domainName+':9898/jolokia/read/*',
dataType:"jsonp",
crossDomain: true,
data: {username: '*',password: '*'},
async: false
}).responseText;
var parsed2 = JSON.parse(jsonData2);
I tried using json p.
The unauthorized code got resolved. It is showing status OK. But now this error comes
Uncaught SyntaxError: Unexpected token u
I think I got the answer. The cross domain issue is resolved.
$(document).ready(function() {
var surl = "http://www.anotherdomain.com/webservice.asmx";
$.ajax({
type: 'GET',
url: surl,
crossDomain: true,
contentType: "application/json; charset=utf-8",
data: { UserID: 1234 },
dataType: "jsonp",
async: false,
cache: false
});
});
Related
My problem is that I can not send the correct request to the server to get an answer from him in the form of a json line. Here is my code:
$.ajax({
type: "GET",
url: url,
dataType: 'json',
crossDomain: true,
username: 'username',
password: 'password',
headers: {
'Access-Control-Allow-Origin': '*'
},
success: function(response) {
console.log(response);
}
})
When I try to send a request, I get an error:
XMLHttpRequest cannot load URL. Response to preflight request doesn't
pass access control check: No 'Access-Control-Allow-Origin' header is
present on the requested resource. Origin 'null' is therefore not
allowed access.
var dataSet = {
username: 'username',
password: 'password',
}
$.ajax({
type: "GET",
url: url,
dataType: 'json',
crossDomain: true,
data: dataSet,
// the origin header has to be set server side
/*
headers: {
'Access-Control-Allow-Origin': '*'
},
*/
success: function(response) {
console.log(response);
}
});
parameters have assigned by a dataSet and the option data.
the allow origin header has to be set server side, not in the client
I'm trying to send a post request using ajax but I keep getting the following error :
XMLHttpRequest cannot load http://192.168.1.123:8080. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access.
here is my code
$.ajax({
type: "POST",
url: "http://192.168.1.123:8080",
data: JSON.stringify([{"VisitorName ": " "+document.getElementById("VisitorName ").value}
]),
contentType: "application / json ",
crossDomain: true,
dataType: "json",
success: function (data) {
alert(data);
},
failure: function (errMsg) {
alert(errMsg);
}
});
$.ajax({
type: "POST",
url: "http://192.168.1.123:8080,
data: JSON.stringify([
{
"
VisitorName ": "
"+document.getElementById("
VisitorName ").value,
}
From what I can tell the comma next to value is causing a syntax error. Also within the code you do not close the " for "http://192.168.1.123:8080
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.
Hey all I have an issue with getting my ASMX web service to work in Chrome and Firefox. Oddly enough it works just fine in IE 11.....
My AJAX:
var sqlQ = "SELECT * FROM vwPS_DAT WHERE OID = 'g643j'";
$.ajax({
type : "POST",
crossDomain : true,
dataType : "json",
async: false,
cache: false,
beforeSend: function (xhr) {
xhr.setRequestHeader("Content-type", "application/json; charset=utf-8");
},
url : "http://zzzzzzz/sqlWS/Service1.asmx/theQ",
contentType: "application/json;charset=utf-8",
data : JSON.stringify({qString : [sqlQ]}),
success : function (data) {
console.log(data);
},
error : function (response) {
alert(response.status + " " + response.statusText + " " + response.responseText);
}
});
What I get from IE:
[{"BC_ACTION":"P","ACTION_DT":"\/Date(1400817600000)\/","BAS_GROUP_ID":" ","DESCR":"Operations".....etc etc..
What I get in Chrome:
Failed to load resource: the server responded with a status of 500
(Internal Server Error).
XMLHttpRequest cannot load http://zzzzzz/sqlWS/Service1.asmx/theQ.
Response for preflight has invalid HTTP status code 500
What am I missing to cause it to not work in Chrome/FF but work in IE11?
I am doing a get request using jquery ajax I am getting data as below How to get only address I dont need the _pronto data
$.ajax({
url: url,
type: 'GET',
contentType: "application/json; charset=utf-8",
success:function(data){
var address= getNormalizedAddress(data) ;
localCache.set(url,address) ;
onSuccess(address);
},
error: wrapErrorHandler(onError, " : Url = " + url + " : Stack = " + new Error().stack),
dataType: 'json',
cache: false,
timeout:defaultTimeoutInMS,
xhrFields: {
withCredentials: true
},
headers: {'Authorization': "53635656"}
response data:
address: Array[151]
__proto__: Object
You just need to get the address field from the returned data:
success:function(data){
var address = getNormalizedAddress(data.address); //not clear whether you will need this
localCache.set(url,data.address) ;
onSuccess(data.address);
}
_proto is present on all objects as it is part of javascript. you can ignore it