how to change the remote API path to localhost using javascript - javascript

I'm accessing the API URL which is located remotely[in US] using the browser. It works fine, displays the API link JSON data(valid json data). When i try to access the same using the AJAX call in the script, it fails with 401 unauthorized error even with valid credentials and key.
I got suggestion to make the path to the API server[remotely located] and configure port number to 81. Can you please suggest me how to configure this.
Script:
$.ajax({
cache: false,
type: "GET",
async: true,
dataType: "JSON",
username: "myemailid",
password: "mypassword",
url: "http://<remotehostipaddress>:82/JSON/Lookups/ComponentTypes",
data: { apiKey: "api key" },
success: function(results) { alert("Success!" },
error: function(xhr) { alert("Error!" }
});

Related

How do i correctly call API with ajax in Cordova?

I have a cordova weather app where I call an API which returns a JSON file with weather data. But every time i call the API with ajax it returns 404, but if i copy the same url in the browser it works.
Here's the part of the code where i call the API(the url is set before):
$.ajax({
type: 'GET',
url: url,
data: {
format: 'json'
},
dataType: 'jsonp',
success: function(data, status){
console.log(data);
},
error: function(data, status) {
console.log(data.status);
}
});
From my comment:
Are you including https:// in the url? or just api... as you have here?
Try https://api.openweathermap.org/data/2.5/weather?q=london&appid=b3a1cd2134e9f28c43beacab95ea8a1a
Cordova or jQuery may or may not prepend http:// or https:// to scheme-free URLS

Why does my JavaScript code get a “No 'Access-Control-Allow-Origin' header is present on the requested resource”

I am trying to do authorization using js by connecting to the RESTful API built in Flask. However, when I make the request, I get the following error:
I know that the api or remote must set the header, but why did it work when I made the request via the Chrome extension?
$.ajax({
type: "POST",
dataType: 'text',
url: api,
username: '',
password: '',
crossDomain : true,
xhrFields: {
withCredentials: true
}
})
.done(function( data ) {
console.log("done");
})
.fail( function(xhr, textStatus, errorThrown) {
alert(xhr.responseText);
alert();
});
Remove crossDomain: true when the API is being served from the same host as the JS...
because it appears to be a bogus warning, caused by this one setting.

Posting data at clients side localhost from server

I would like to POST some JSON data at clients side localhost from my Web Application.
I tried using localhost but this refers to the Server Side - Localhost. I also tried to use Javascript instead of PHP but this didn't work as well.
The goal I am trying to achieve is to be able to send some data to the clients localhost where an other desktop application will be listening to a specific port and then do some work (like printing a label) with the given data.
Code I tried:
(I am using XAMPP as a localhost test server)
$.ajax({
type: 'post',
url: 'localhost',
dataType: 'json',
data: 'json',
success: function(json) { ... },
error: function(error) { ... }
});
are you referring to a server and client within a local private network? If so, you can just specify the IP address and port of the client when sending the post request.
Ex:
$.ajax({
type: 'post',
url: '192.168.xxx.xxx:8080',
dataType: 'json',
data: 'json',
success: function(json) { ... },
error: function(error) { ... }
});

Ajax cross domain POST in IE8 and IE9

I am trying to make a ajax cross domain POST in IE9 and IE8 to a rest endpoint that resets a password and am not having any luck. The ajax call I am trying to make is as follows:
$.ajax({
type: 'POST',
crossDomain: true,
url: postUrl,
contentType: 'application/json; charset=utf-8',
data:{ "userid":uid, "email":email, "password":password },
dataType: 'jsonp',
success: function (data) {
console.log(data);
},
error: function (status){
console.log(status);
}
});
I am also making sure to include the following before the POST call:
$.support.cors = true;
Everytime I make the call it goes into the error function. It says the status is success however when I try to login with the new password it is not working which means it wasnt a success. The following is the response that I output to the log:
[object Object]{readyState: 4, status: 200, statusText: "success"}
Also note that I am calling the jQuery.XDomainRequest.js by MoonScript. That several other stack overflow answers say to do. And my jquery version is 1.11.3
Can someone please provide me some direction on this.

Ajax request failing?

I am trying to retrieve json data from an api.
It keeps failing, but when I look in the Net tab of Firebug I can see that the GET request executed and returned the correct data. Am I doing something wrong or does anyone have tips on how to debug this?
Edit: Have changed to dataType json and error status code is 0
Thanks
$.ajax({
url: 'http://localhost:55894/api/Test/All',
data: {
format: 'json'
},
error: function () {
alert('Error');
},
dataType: 'jsonp',
success: function (data) {
alert('Ok');
},
type: 'GET'
});
From the info you provided the reason it is failing is because you dont have a cross domain access policy setup. Because you are using different ports to host the website and the API you are running into this issue. You can either setup a crossdomain.xml with the proper security settings or you can move both the API and the webserver to the same port.
Have a look at this for more info: http://en.wikipedia.org/wiki/Same-origin_policy
u can try this way:
$.ajax({
type: 'GET',
url: 'url api here',
beforeSend: function() {
},
success: function(data) {
},
error: function(xhr) { // if error occured
},
complete: function() {
},
dataType: 'json'
});
JSON and JSONP are different. If you are using JSONP, the server side must be prepared to support it. Doesn't look like you are using JSONP.
So just change dataType to 'json', as you are "trying to retrieve json data".

Categories

Resources