Calling Restful service through jQuery Ajax - ajax

I am trying to access a restful web service using jQuery Ajax but getting following error:
XML Parsing Error: no element found Location: moz-nullprincipal:{cefa3a59-2437-454f-b39a-384cf1fdf072} Line Number 1, Column 1:
This how I am making the call:
function getResponse(){
$.ajax( {
type:'Get',
dataType:'xml',
url:'http://localhost:8080/RestTest/restservice/number',
success:function(data) {
alert(data);
}
} );
}
Here my response data type is xml. I understand that there is some cross domain issue but not sure how to resolve it. Please help me on this.

Use getJSON
$.ajax({
url: url,
dataType: 'json',
data: data,
success: callback
});

Related

Ajax jsonp missing ; error

I am near a solution to get json data, just get the error :
SyntaxError: missing ; before statement
My json data look like this :
{"custom1":"","dateOfBirth":null,"custom2":"","custom3":"","custom4":"","custom5":""}
I am using jsonp. this solved my problem with CORS (Cross-origin request blocked)
In the debugger i can see the data in the NET.
How close i am?
Som get me the last push to resolve my problem??
<script type="text/javascript">
function getdata() {
$.ajax({
type: 'GET',
url: 'http://myurl/api/getUser/22?_key=123456789',
dataType: 'jsonp',
//contentType: "application/jsonp",
success: function (data) {
$('#badge').append(result);
}
});
}
You have to set your ajax response type to jsonp instead of json.
Like
dataType: 'jsonp'
You can read more about it in jquery docs on $.ajax. Also as I can see, the result variable that you are trying to append in the success callback is not declared & inititalized anywhere in the code you have provided.

cross domain request issue origin not allow

cross domain request issue
my ajax call code actually when i am running given url directly in browser it shows me json data but using ajax call it always shows me ajax error believe me i tried n follow many things no fruitful result from 18 days.
$.ajax({
crossDomain:true,
type: "GET",
url: 'http://taxihub.azurewebsites.net/Mobile/api/json.php?method=getCompanyList',
contentType: "application/json; charset=utf-8",
dataType: "json",
processData: false,
success: function (data) {
console.log(data);
return;
},
error: function (err) {
console.log("AJAX ERROR");
console.log(err.responseText);
}
});
you can also check this link directly u will see json data coming but i found error i dont know why
"http://taxihub.azurewebsites.net/Mobile/api/json.php?method=getCompanyList"
error: OPTIONS http://taxihub.azurewebsites.net/Mobile/api/json.php?method=getCompanyList Origin lhost:809 is not allowed by Access-Control-Allow-Origin.
please help me i am stuck here from 18 days on this issue
Remove the cross-domain and content-type part and it'll work but you'll have to do a little extra string manipulation work to put it in JSON object:
$.ajax({
type: "GET",
url: 'http://taxihub.azurewebsites.net/Mobile/api/json.php?method=getCompanyList',
dataType: "json",
success: function (data) {
console.log(data.result.getCompanyList[0].CompanyID);
return;
},
error: function (err) {
console.log("AJAX ERROR");
console.log(err);
}
});
}
Actually it seems cross-domain doesn't have any effect with or without it. I guess the server you're targeting doesn't have strict cross-domain prevention but doesn't like the JSON content-type. I'd be glad to get a more specific explanation.
EDIT I used JQuery 1.10.2 if it matters

Uncaught SyntaxError: Unexpected token : , when i access tickets.com json api

I'm trying to get data from:
http://api.master18.tiket.com/search/autocomplete/hotel?q=mah&token=90d2fad44172390b11527557e6250e50&secretkey=83e2f0484edbd2ad6fc9888c1e30ea44&output=json
I'm using the jQuery $.ajax method and the code is written in my index.html file:
function getAPI() {
jQuery.ajax({
url: 'http://api.master18.tiket.com/search/autocomplete/hotel?q=mah&token=90d2fad44172390b11527557e6250e50&secretkey=83e2f0484edbd2ad6fc9888c1e30ea44&output=json',
type:"get",
dataType: 'jsonp',
crossDomain: true,
jsonp: false,
success: function(data) { console.log(data); }
});
}
When I try it in the Chrome console, there is a message
Uncaught Syntax Error:Unexpected token:
I am very confused about what the problem is, can anyone help me?
The content being returned is json, not jsonp as you specified in your dataType. Also, by setting jsonp to false, you're preventing a jsonp querystring from being appended to the URL by jquery, which is likely why the API you're using is returning json and not jsonp. I would try taking out the jsonp: false parameter.

jQuery Ajax request in Grails

How to use jQuery to make Ajax request in Grails pages?
How to setup a URL hitting a method on Grails Controller? Let's say controller:'airport', action:'getJson' and input to the action is 'iata'.
I am able to set static url as http://localhost:8080/trip/airport/getJson but not able to figure out how to pass input for iata specifically.
I am fairly new to Grails and following IBM's 'Mastering the Grails' tutorial series. Do suggest me some good tutorial on using jQuery with Grails.
use the $.ajax method in jquery
$.ajax({
url:"${g.createLink(controller:'airport',action:'getJson')}",
dataType: 'json',
data: {
iata: '.............',
},
success: function(data) {
alert(data)
},
error: function(request, status, error) {
alert(error)
},
complete: function() {
}
});
It's:
$.ajax({
url: '/trip/airport/getJson',
data: {paramName: 'iata'}
});
use your parameter name, that you're expecting in action, intead of paramName, that i've used.

How use Facebook Javascript SDK response in Ajax request?

Supposing I have the following code which returns a Javascript object which I can read in Firebug's console:
FB.api('/me',function(apiresponse){
console.log(apiresponse);
});
How can I then use the data from apiresponse in an Ajax request on the same page?
Currently my Ajax request looks as follows:
$.ajax({
// CodeIgniter URL
url: "<?=site_url?>('login/add_fb_users'); ?>",
type: 'POST',
data: apiresponse,
success: function(data) {
alert(data);
}
});
I know very little about Javascript, but reading around the subject leads me to think I have to convert the Javascript object to a JSON string. Is that correct? Am I on the right track?
You could put your AJAX call inside the handler for the API call like below..
FB.api('/me', function(apiresponse){
console.log(apiresponse);
$.ajax({
// CodeIgniter URL
url: "<?=site_url?>('login/add_fb_users'); ?>",
type: 'POST',
data: apiresponse,
success: function(data) {
alert(data);
}
});
});
one possible way:
define a global variable in your javascript, e.g. var myVar1;
set apireponse to the global variable in your FB.api callback (i.e. where u call console.log)
reference the var myVar1 in your ajax fcn.

Resources