getJson ajax request - getjson

Please help,
I have been trying to get Json data but with no avail.
I have managed to get it working in dreamweaver but once uploaded to my domain it then does not work.
I have looked at many examples but cant get mine to work. I have seen examples that get picturs from flikr that work on my domain.
I need to get the json data from http://api.open-notify.org/iss-now/
This works in dreamwaever
$.getJSON('http://api.open-notify.org/iss-now/',
function(json) {
$('#images1').text(json.timestamp);
});
and this
$.ajax({
type: 'GET',
url: MyUrl,
dataType: "json",
success: function(data){alert("Success");hello(data);},
error: function(XMLHttpRequest, textStatus, errorThrown){alert("Error : "+" ':' "+textStatus+" ':' "+errorThrown);},
});}
is this a cross domain issue? why does the Flikr ones work as this is cross domain too? please help.
Many Thanks.

cross domain should be done using JSONP, checkout this question, and the JSONP section here.

Related

net::ERR_CONNECTION_ABORTED error mysteriously disappears with axios post request

I was attempting to make a post request with FormData to my local server using axios inside React. Here is the method that was posting to the server:
uploadHandler = () => {
const formData = new FormData();
formData.append(
"myFile",
this.state.selectedFile,
this.state.selectedFile.name
);
axios({
method: "post",
url: "/",
data: formData
}).then(response => {
});
};
For the last three hours, I kept getting this error in my chrome console:
net::ERR_CONNECTION_ABORTED when firing the method. I could not figure out what was wrong and tried adding various headers to the request, but nothing worked.
Then, all of a sudden, it just WORKED. I had no idea why and hit ctrl z until I could pinpoint the exact change that got it to work. The hunt turned up nothing and now I can't get the error no matter how hard I try. This is driving me crazy not knowing what the issue was that took 3 hours of my day!
Has anyone experienced something similar? Am I missing something here? Any input is greatly appreciated!

Atlassian Jira - 401 only when using query parameters

Currently working on a JIRA addon using the ACE framework. Executing a request using the integrated httpClient.
When I make a request such as this
https://instance.atlassian.net/rest/api/3/search
it works fine using the header Authorization: JWT <token> but when I run the same request with a query parameter like this
https://instance.atlassian.net/rest/api/3/search?maxResults=1
the request fails with a 401. I have confirmed that the JWT is not expired due to reverting the query parameters and seeing success again.
My atlassian-connect.json has scope READ as requested by the endpoint.
Any suggestions?
I was surprised that the rest call "rest/api/2/search?maxResults=1" worked. But it did when I was logged into my instance.
If I try that as JQL in Issue Search (maxResults=1), I get an invalid or unauthorized error message.
My instance is on premise (API V2). Yours appears to be in the cloud (V3). So it may be that the REST search works more like the Issue Search in V3 and is therefore returning the 401
It's a guess that should be easy to test... replace your maxResults=1 with some actual JQL or a filter ID and see if your results change
Since you're using ACE and utilizing httpClient, you might want to try the checkValidToken() route instead. The snippet below worked for me.
app.get('/mySearch', addon.checkValidToken(), function(req, res) {
var httpClient = addon.httpClient(req);
httpClient.get({
url: '/rest/api/3/search?maxResults=1',
headers: {
'X-Atlassian-Token': 'nocheck',
'Content-Type': 'application/json'
}
},
function (err, httpResponse, body) {
if (err) {
return console.error('Search failed:', err);
}
console.log('Search successful:', body);
});
});

Ember js RESTAdapter PUT request adds .json to the end

I've been trying to learn Ember and I have a question.
In my store I'am getting data from .json like below. I have tried without buildUrl function but cant load the json file, then found this solution on SO.
CocktailApp.Store = DS.Store.extend({
revision: 12,
adapter: DS.RESTAdapter.extend({
bulkCommit: false,
url: "http://localhost:8888",
buildURL: function(record, suffix) {
var s = this._super(record, suffix);
return s + ".json";
}
})
});
Now comes my question: When I commit the chances (by pressing add to favs or remove from favs) RESTAdapter adds ".json" at the end of to PUT request. See the below code and screenshot
CocktailApp.CocktailController = Ember.ObjectController.extend({
addToFav: function () {
this.set('fav',true);
this.get('store').commit();
},
removeFromFav: function () {
this.set('fav',false);
this.get('store').commit();
}
});
I think thats why my PUT request can not be handled. But If I remove the builtURL function no json loaded at all. How can I resolve this problem?
Thanks
If the API endpoint url does not require .json at the end of it, then remove that line from your buildURL function. My guess is that the example code you got was consuming a ruby on rails api, or something similar.
remember, when you send a GET, PUT, POST, or DELETE to a url, that url needs to actually be a real endpoint. You can't just add extraneous stuff to it and have it still work.

FB API: Resource interpreted as Script but transferred with MIME type text/html

I'm using FBJS to post photo
FB.api('me/photos', 'post', {
message: 'some message',
url: 'some url'
}, function(response){
if (!response || response.error) {
/*some error alert*/
else {
/*some success*/
}
}
});
Almost, it works fine, but OCCASIONALLY, it shows error alert.
I see on Console, it says: Resource interpreted as Script but transferred with MIME type text/html
How to deal with it?
I've tried some solutions (such as Content-Type, script type...) but it still does not works in SOME times :(
Please help me.
I had that problem and in my case the reason was local - the "Disconnect" Chrome addon, that was blocking requests to facebook, I just forgot that I had it installed.
Have you tried
headers: [
{ "name":"Content-Type",
"value":"text/javascript; charset=UTF-8"}
]
If it is only occasional, maybe you're hitting an API call limit and it's returning an error? Check that the data is correct, even if it is getting an error. Use fiddler to get more clear diagnostic results.

Origin is not allowed by Access-Control-Allow-Origin for HTTP DELETE

I currently playing around with the Facebook JavaScript SDK and the Scores API ( https://developers.facebook.com/docs/score/ ). I wrote a small application to save (post) scores and now I want to delete scores. Posting (saving) them works fine.
My code looks like this:
var deleteHighScoreUrl = 'https://graph.facebook.com/'+facebook.user.id+'/scores?access_token='+facebook.application.id+'|'+facebook.application.secret;
jQuery.ajax(
{
type: 'DELETE',
async: false,
url: deleteHighScoreUrl,
success: function(data, textStatus, jqXHR)
{
console.log('Score deleted.');
}
});
The "facebook" variable is an object that holds my application data. For HTTP POST it works fine but for HTTP DELETE I get the response "NetworkError: 400 Bad Request" in Firebug (with Firefox 10). I saw that Firefox first sends an HTTP OPTIONS (to see if it is allowed to use HTTP DELETE) which leads to this error so I tried the same thing with Google Chrome. Google Chrome sends a real HTTP DELETE which then returns:
"XMLHttpRequest cannot load
https://graph.facebook.com/USER_ID/scores?access_token=APP_ID|APP_SECRET.
Origin MY_DOMAIN is not allowed by Access-Control-Allow-Origin".
I think that this is a classical cross domain issue but how to solve it? I've added my domain to my facebook application (at https://developers.facebook.com/apps) and Facebook has a paragraph which is called "Delete scores for a user". So it must be possible to delete the scores (somehow)?
Because of Cross-Site-Scripting (XSS) a HTTP DELETE is not possible. But you can send a HTTP POST request with the query parameter ?method=delete, which then deletes the score.
Code Sample:
Facebook.prototype.deleteUsersHighScore = function()
{
var deleteHighScoreUrl = 'https://graph.facebook.com/'+this.user.id+'/scores?access_token='+this.application.id+'|'+this.application.secret+'&method=delete';
jQuery.ajax(
{
type: 'POST',
async: false,
url: deleteHighScoreUrl,
success: function(data, textStatus, jqXHR)
{
console.log('Score deleted.');
}
});
}
This is the Cross Domain security issue.
The fact that your error contains the message "Origin MY_DOMAIN" would tell me that somewhere in your code you have copied one of Facebook's examples but not changed the value for MY_DOMAIN to the correct domain you are using.
I would check all of your code for the value "MY_DOMAIN".
Please ignore this advice if you have changed the value to hide your actual domain in your question.