I'm having difficulty getting $.ajax to work with my webservice.
I have a webservice I wrote in Java (and I suck at Java) which expects urls like example.com/webservice/somestuff/callback
and returns JSONP like callback({"success":true})
My jquery resembles:
$.ajax({
type: "GET", dataType: "jsonp",
url: '//example.com/someStuff',
}).done(doSomething)
The problem is, all the nice stuff jQuery does to make JSONP work is messing me up. It appends a querystring (i.e. ?callback=jQuery19786...), where my Service expects just a regular path parameter.
If I could somehow convince jQuery to do example.com/webservice/somestuff/jQuery19786... that would be ideal.
Ideas?
Just place ?? where you want the callback to be placed.
$.ajax({
type: "GET", dataType: "jsonp",
url: '//example.com/someStuff/??',
}).done(doSomething)
An alternative is to use the beforeSend option to manipulate the url after jQuery modifies it:
$.ajax({
url: "//example.com/foo/bar/?callback=?",
dataType: "jsonp",
cache: true,
beforeSend: function(jqXHR,options){
options.url = options.url.replace(/\?callback\=/,"");
//console.log(options.url);
//return false;
}
});
Related
So I've been trying to use the time_filter parameter to get a list of my current and future Events (so no past events) but instead I'm getting all the events; so that parameter is not working for me. Is weird because other parameters like 'expand=venue' do work, but when I try for example time_filter=current_future it doesn't work and instead I still receive all events including those events that has expired.
Here is the AJAX code I'm using to call the API:
$.ajax({
type: 'GET',
url: 'https://www.eventbriteapi.com/v3/users/me/events/?time_filter=current_future&expand=venue&token=MYTOKEN',
dataType: 'json',
contentType: 'application/json'
}).done(function (result) {
//result code goes here
});
Has anyone here successfully used the time_filter parameter and if so what am I doing wrong? because that parameter is the only one that the GET call ignores cause all the others I've used work. Any help is appreciated.
Thanks!
Found the solution. Removing contentType: 'application/json' solves the issue. I'm not sure why, maybe is cause since I'm not sending data then it perhaps steps in the way of the parameters I'm trying to send. Either way, for those using Eventbride API and are not sending custom data and also want to use the time_filter parameter; then removing contentType: 'application/json' will solve the problem.
Here is the working code:
$.ajax({
type: 'GET',
url: 'https://www.eventbriteapi.com/v3/users/me/events/?time_filter=current_future&expand=venue&token=MYTOKEN',
dataType: 'json'
}).done(function (result) {
//result code goes here
});
I am using an AJAX request and Handlebars to communicate the data transfer as shown below.
main.hbs
<script>
..
$.ajax({
type: "POST",
data: JSON.stringify({
propId: "{{staysAt}}",
propImg: "/{{propImg}}",
_csrf: "{{csrfToken}}"
}),
contentType: 'application/json',
dataType: 'json',
url: "/user/sendRequest",
success: function(result){
},
error: function(err){
//console.log(err);
}
})
..
</script>
In propImg data is being stored as
/uploads 8-11-27T10-54-19.118Zparis-graph-colored.png
Instead of
/uploads\2018-11-27T10-54-19.118Zparis-graph-colored.png
When i use main.hbs to display the Handlebar object outside the AJAX request it works perfectly
<h2>/{{propImg}}</h2>
and gives the complete image path, however inside the AJAX call I do not understand what is going wrong.
I tried using {{{propImg}}} for no escape as well however the same issue is coming.
Here is my little first project as practice, having real trouble figuring out how to use JSON, took me a while to get it work locally but still no luck with servers, and tried few a including one i hosted, and even tried it with other hosted json files.
http://jsfiddle.net/Atlas_/Mgyc5/1/
$.ajax({
dataType: "json",
async: false,
url: "package.json", //https://www.dropbox.com/s/fmw63i4v7dtnx6t/package.json
'success': function (json) {
theQuiz = json.quiz;
console.log(json);
console.log(theQuiz);
}
});
When you access another domain be carefully with "Crossdomain".
To use with dropbox, try changing the URL to:
https://dl.dropboxusercontent.com/s/fmw63i4v7dtnx6t/package.json
Your code will be like this:
$.ajax({
dataType: "json",
async: false,
url: "https://dl.dropboxusercontent.com/s/fmw63i4v7dtnx6t/package.json",
success: function (json) {
theQuiz = json.quiz;
console.log(json);
console.log(theQuiz);
}
});
When you request 'dl.dropboxusercontent.com', you have this:
Access-Control-Allow-Origin: *
https://developer.mozilla.org/en-US/docs/HTTP/Access_control_CORS?redirectlocale=en-US&redirectslug=HTTP_access_control
"In this case, the server responds with a Access-Control-Allow-Origin:
* which means that the resource can be accessed by any domain in a cross-site manner."
Another options: Some websites (twitter, for example) work with "jsonp". http://en.wikipedia.org/wiki/JSONP or ..you can create your own proxy.
I'm working with Github API V3
I'm using following code to make ajax call
$.ajax({
type:'POST',
url: 'https://api.github.com/gists',
data: JSON.stringify({
"public": true,
"files": {
"sample.html": {
"content": 'html content'
}
},
}),
success:function(response){
alert(response.id);
}
});
I have to stringify data as Github API returns Error 400! if i don't. With above example, Github API does response as I expect.
I'm having issue with callback parsing though. Above code works with webkit & opera but firefox fails with success function. I have to modify code as below to get work in firefox.
success:function(response){
alert(JSON.parse(response).id);
}
But then Webkit & Opera fails with success response with above modified code.
What is correct way to get success callback across all browser ? What I'm doing wrong ?
Add dataType: 'json', property to your ajax call check if it works..like the one below
$.ajax({
url: url,
dataType: 'json',
data: data,
success: callback
});
I want to send a request with parameters as json. I tried to set these in request body but this doesn't work. Is there any other way to send parameters as JSON?
Thanks,
$.ajax({
type: 'GET',
url: url,
data: JSON.stringify(myJsonObject),
success: successCallback,
dataType: 'json'
});