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
});
Related
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.
I am trying to call an asmx web service in plain html page. But not getting output. I'm not getting the exact problem. Here is my ajax code that I wrote to call that asmx web service.
function Getdet(Name)
{
alert('hellotest');
$.ajax({
type: "POST",
url: "http://192.168.1.20/myservice/service.asmx?HelloWorld", // add web service Name and web service Method Name
data: "{''}", //web Service method Parameter Name and ,user Input value which in Name Variable.
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response)
{
alert('abcd1');
$("#spnGetdet").html(response.d); //getting the Response from JSON
},
failure: function (msg)
{
alert(msg);
}
});
}
Should I have to add anything in the code..?? Please let me know where I am going wrong in this above code.
ASMX pages are plain old Web Services and the output is (almost) always in XML format, so don't except JSON out of the box.
Plus, they don't work well on different domain call by default, you will need to ensure that the cross domain is correctly handled.
After all this is done, you will then use the call as:
success: function(response) {
// http://stackoverflow.com/a/1773571/28004
var xml = parseXml(response),
json = xml2json(xml);
$("#spnGetdet").html(json.d);
}
I am trying to pull in some external JSON phone list for a test I am doing locally.
JSON = https://www.o2.co.uk/shop/homepage/scripts/phoneList.json
I have read on a seperate thread that to overcome allow origin / cross domain issues you can pass 'callback=?' to the end of the url. This seems to work fine as now in the devtools network tab I can see the file being pulled in fine.
However, I never get it to go into the success callback function. It always brings back a parsererror.
The code is 100% valid according to jsonlint.com
A snippet of the code as below:
$.ajax({
type: 'GET',
url: 'https://www.o2.co.uk/shop/homepage/scripts/phoneList.json?callback=?',
contentType: 'application/json; charset=utf-8',
cache: true,
dataType:'json',
success: function(response) {
console.log(response);
},
error: function(jqXHR, textStatus, errorThrown) {
console.log(jqXHR);
console.log(textStatus);
console.log(errorThrown);
}
});
Any ideas welcome as I am well and truly stuck.
Thanks.
JSONP only works if the server supports it. What the server needs to do, is take your callback parameter and wrap the json data in a function call with that name.
So when https://www.o2.co.uk/shop/homepage/scripts/phoneList.json responds with
[{
"handset":"Alcatel 10.30 Black",
"link":"/shop/phones/alcatel/10.30-black/"
}]
https://www.o2.co.uk/shop/homepage/scripts/phoneList.json?callback=MyCallback should respond with:
MyCallback([{
"handset":"Alcatel 10.30 Black",
"link":"/shop/phones/alcatel/10.30-black/"
}]);
If the server doesn't do that, JSONP can't work. Perhaps O2 doesn't want you using their data.
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;
}
});
so I can't seem to get my dropdown menu to work. Here's the code:
$.ajax({
url:"/json/testjson",
cache:true,
success: function(j){
console.log(j);
alert(j);}
});
This returns a valid json string:
{"data": ["test", "data"], "result": "OK"}
I can't get the success function to fire at all. There is no error anywhere (neither in the firebug log, nor in the netlog, and the response from the site is okay. The firebug parser cannot parse that JSON but the JLINT validator validates it correctly. I have tried the setting the content-type to application/json, no success. I have also tried, $.getJSON, $.get, and nothing is firing. I have also tried
Any ideas?
Thanks
Jon
Thank you HaBo - the answer was to remove the cache: true!
Thanks!
J
I believe you need to set dataType to json to make sure the data coming from the server is treated as a json object.
$.ajax({
url: "/json/testjson",
dataType: "json",
cache: true,
success: function(j) {
console.log(j);
alert(j);
}
});
Take a look at the $.ajax API.