I made this function to request some tickets from JIRA,I gave the data type as jsonp to avoid the Cross origin problem , and when I make the request I get the response in browser debugger , but cannot handle the json response "SyntaxError: missing ; before statement" , is there any way to read json if I send the request for jsonp ?
var ajaxUrl = jira/rest/api...
jQuery.ajax({
url:ajaxUrl,
dataType: 'jsonp',
jsonpCallback: 'callback',
type : "GET",
beforeSend: function (xhr){
success: function w(data){
console.log(data);
alert('Sucess data: ' + data);
};
error: function e(data){alert('alert error');}
}
})
I solved the problem , I changed the jsonp in json , and I remade the corss from jira server how it is explained here : https://community.atlassian.com/t5/Answers-Developer-Questions/Enable-CORS-in-JIRA-REST-API/qaq-p/553997 , it looks like the problem was on the preflight part.
Related
I am creating a site in which user can create popup on one site and then take small code to used on other site . Problem is when i am made ajax call from other site to fetch data in others site database i am getting error "No cross origin access allowed".When i am using jsonp it is giving me correct response but i am not able to collect it. It is giving me error"Unexpected token". I am using jquery ajax if you have any other idea then let me know i will implement it also. Here is my code :
$.ajax({
url: "url",
// Tell jQuery we're expecting JSONP
dataType: "jsonp",
jsonpCallback: 'callback',
type: 'GET',
data: {
format: "json"
},
// Work with the response
success: function( response ) {
console.log( response ); // server response
}
});
If Your response has error server side Cross domain issue then your server side response should exist Access-Controll-Allow-Origin with * value will solve your problem.
You can try that:
crossDomain : true,
$.ajax({
url: "url",
// Tell jQuery we're expecting JSONP
dataType: "jsonp",
jsonpCallback: 'callback',
crossDomain : true,
type: 'GET',
data: {
format: "json"
},
// Work with the response
success: function( response ) {
console.log( response ); // server response
}
});
It solved my problem here.
I'm trying to make a cross domain ajax request to deezer, a music streaming api... and I'm getting an "Uncaught SyntaxError: Unexpected token :" error. I know it's because the data I'm getting back is not in proper json format, but I tried changing the datatype to 'json' from 'jsonp' and it's still not working...here's my request, any suggestions?
$.ajax({
url: "https://api.deezer.com/search?q=" + searchString + "&callback=?",
dataType: 'json',
jsonpCallback: 'callback',
type: 'GET',
success: function (data) {
console.log(data);
}
});
This is because you used unencoded second "?" in your url after first "?", which using for separate get-string. You need to remove this part of url + "&callback=?".
I am trying to do a GET request to solr search engine using $.ajax() from jQuery. This is the code I am using for doing an ajax call:
$.ajax({
url : 'http://xxx.xxx.xxx.xxx:port#/solr/mycore/select?indent=on&q=myparam:value&wt=json',
type: "GET",
dataType: "jsonp",
jsonp : "callback",
success: function(data) {
console.log("Success", data);
},
error: function(data) { alert("Error"); }
});
So I am getting a valid json object in the response but somehow the browser throws an Uncaught Syntax Error. The actual error is:
Uncaught SyntaxError: Unexpected token :
select?indent=on&q=myparam:value&wt=json&callback=…....somevalue...
The tricky part is that the response header is text/plain when I checked in the browser. How can I solve this? Please help me...
Colons require encoding in query strings.
Your url should look like this:
http://xxx.xxx.xxx.xxx:port#/solr/mycore/select?indent=on&q=myparam%3Avalue&wt=json
If you're generating the query string dynamically, use encodeURIComponent() to correctly encode special characters.
I got this solved. Actually I had to use jsonpCallback:"mysuccesscallbackfunction" in the ajax call and json.wrf=mysuccesscallbackfunction in the URL. It looks like this now:
$.ajax({
url : 'http://xxx.xxx.xxx.xxx:port#/solr/mycore/select?indent=on&q=myparam:value&wt=json&json.wrf=mysuccesscallbackfunction',
type: "GET",
dataType: "jsonp",
jsonp : "callback",
jsonpCallback:"mysuccesscallbackfunction",
success: function(data) {
console.log("Success", data);
},
error: function(data) { alert("Error"); }
});
and my mysuccesscallbackfunction is:
function mysuccesscallbackfunction(resp) {
console.log('inside mysuccesscallbackfunction: ', resp );
}
Now, first it executes whatever is inside mysuccesscallbackfunction and then goes to default success callback. I found it somewhere on web. I would still like to know why it worked now.
I'd like to ask a question for cross-domain Get request via Ajax.
My ajax request is Like that
var currency_path="http://forex.cbm.gov.mm/api/latest";
$.ajax({
url: currency_path,
crossDomain:true,
type:"GET",
dataType:'jsonp',
async:false,
success: function(data){
console.log(data);
},
error: function(){
alert('failure');
}
}).done(function(msg) {
console.log(msg);
});
I got the response but i can't trace that
Any Suggestion ?
Look in your JavaScript error console:
Uncaught SyntaxError: Unexpected token :
You have dataType:'jsonp', but the URL is returning JSON.
You can't parse JSON as JSONP, there are different data formats.
Use some other technique to access the data.
I am using Phonegap to connect to a server to authenticate a user. The server expects the data to be Json encoded, and I will get the response back from the server.
var serviceURL = "http://appdev/PEPS-CS-Services/";
The serviceURL (APPDEV) is hosted on our network within the office.
var loginData = {
'strUniqueID' : '123',
'UserName' : 'User123',
'Password' : 'Password123'
};
I have checked the login credentials and they are fine. When I try the following:
$.ajax({
type: "POST",
url: serviceURL + "services/AuthenticationServices/authenticateUser",
data: loginData,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(data) {
alert(data);
},
failure: function(errMsg) {
alert(errMsg);
}
});
I get the Access-Control-Allow-Origin error in my console.
XMLHttpRequest cannot load http://appdev/PEPS-CS-Services/services/AuthenticationServices/authenticateUser.
No 'Access-Control-Allow-Origin' header is present on the requested resource.
Origin 'http://localhost' is therefore not allowed access.
I had a look around, and I was suggested to use Jsonp as the dataType in the ajax request, which does give me back the response I was looking for. However, it isn't alerted out as it should be, the console shows the url with the parameters used in the loginData variable, as below
Resource interpreted as Script but transferred with MIME type application/xml:
http://appdev/PEPS-CS-Services/services/AuthenticationServices/authenticateUser?callback=jQuery164017807046906091273_1396515434941&strUniqueID=123&UserName=Mark&Password=Password1&_=1396515434963"
When I open the link in a browser i get the correct response, as below
<ns:authenticateUserResponse xmlns:ns="http://services">
<ns:return>SESSION ID HERE</ns:return>
</ns:authenticateUserResponse>
However, I also get the Uncaught SyntaxError: Unexpected token < error below it.
I have tried to change to data: loginData to data: {'data':$.toJSON(loginData)}, but still the same error.
My questions are the following:
Am I sending the data over correctly? Why does the jQuery164017807046906091273_1396515434941 get added to the URL before the parameters?
And why am I getting the Uncaught SyntaxError too?
Is this because the server is sending back the incorrect format of the data? It is currently sending back XML
Any help would be greatly appreciated, thanks
This is a familiar cross context issue, you are not allowed to request resource from another domain with simple ajax call and expect a result.
Instead, use a jsonp call and regiter a callback function to call when return result:
var success = function(data){
/* parse JSON */
data = $.parseJSON(data);
/* show json parsed data */
alert(data);
};
$.ajax({
url: serviceURL + "services/AuthenticationServices/authenticateUser",
dataType: 'jsonp', //use jsonp data type in order to perform cross domain ajax
crossDomain: true,
data: loginData,
success: success,
error: function(errMsg) {
alert(errMsg);
}
});