Consume Restful request JSON - javascript

When I test the code below it only fails
Any Ideas? The link works fine
$.ajax({
url: 'http://ulacit3352.cloudapp.net/Login/webresources/generic/search/gera',
type: 'GET',
datatype: 'json',
success: function(data) {
alert("works")
},
error: function() {
alert("it does not");
}
});
I get this on Chrome:

The response of the link is not of type "json", instead, it is a plain text, therefore it cannot be parsed. You should change to
dataType: 'text',
Also, for normal ajax, you need to make sure the url is in the same domain of the webpage, which means the code should reside at http://ulacit3352.cloudapp.net/ as well. Otherwise, you should seek for "jsonp" or (better) some server-side solution, such as setting Access-Control-Allow-Origin or make your server as a proxy of the request.

The requested url is server on http which will give rise to This request has been blocked; the content must be served over HTTPS error since the connection is open for eavesdropping and man-in-the-middle (MITM) attacks. It is better to use https provided the url accept https request.
Still there is way to bypass the issue. You can check this LINK

Related

Send Ajax Request For Sending Post Data One Url to Another Url Without Getting CROS ERROR

-> Please Help Me To Solve The Problem
-> I Send Data To Another URL for Process Something And That Web Page is another Server and Different URL.
-> When Ever I Try To Send Post Data That time i Faced CROS Error i try many ways.
-> See My Code
$.ajax({
type: 'POST',
url: 'Another Non Https Url',
crossDomain: true,
contentType:'application/json',
data:{CompanyEmail:CompanyEmail,CompanyName:CompanyName,CompanyPhoneNumber:CompanyPhoneNumber,AccessToken:AccessToken},
success: function(){
alert("Success");
},
error: function(){
alert("Failed");
}
})
CORS (meaning "Cross Origin Resource Sharing") is a server-side mechanism to allow exceptions in the so-called Same-Origin-Policy. Otherwise processing data from another origin is prevented.
You have to explicitly enable CORS for your client within your server.
This can't be fixed in your client-side jQuery-Code.
PHP-side you are able to activate CORS for all clients by using
<?php
header("Access-Control-Allow-Origin: *");
?>
though.

Looking for clarification; How do I access a particular webservice with local files an Ajax

It could be I have completely misunderstood how web services work.
So there's a webservice I want to access through a javascript file sitting in a folder on my desktop. The server I am trying to access has not set a CORS header, so I have some trouble just using regular ol' ajax.
And frankly I am not sure where to go from here.
$.ajax({
type:"get",
crossdomain: "true",
url: "https://name", //actual name redacted from this question
success: function(data){
console.log(data);
}
});
A regular get like this will print the following in firefox's console.
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://ws.zooom.no/v1/channels. (Reason: CORS header 'Access-Control-Allow-Origin' missing).
I got a hint from the task giver that I need to set the following in my "hosts file"
#127.0.0.1 www.domainname.com
Which confuses me. I have little to no experiences hosting anything, but does this hint that I need to maybe virtually host my "webpage" to access the webservice? It probably doesn't help to say I feel at a loss here.
So the bad news is that unless the 3rd party service either allows cross-domain requests (this is a configuration on that 3rd party server), or allows JSONP requests, you really have no option to access this service using a browser and javascript.
You could perhaps build your own service to proxy requests to that third party service. This would give you control over domain, CORS, JSONP, etc.
I found a solution.
Using something called "JSONP" that I had never heard of until now, i wrote a request like this
$.ajax({
type: 'GET',
url: "https://DOMAINNAME.com",
contentType: "application/json",
dataType: 'jsonp',
success: function(data) {
console.log(data)
},
error: function(e) {
console.log(e.message);
}
});
This somehow completely negated the CORS problems

Cross-domain GET request, difference between browser and localhost making the call

I attempt to make a GET request to an API from a locally hosted meteor app (=> App running at: http://localhost:3000/) and upon doing so I get the error:
"XMLHttpRequest cannot load [the-api-url]. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin http://localhost:3000 is therefore not allowed access."
Yet when I paste [the-api-url] into my browser and hit ENTER, the appropriate API response is shown in my browser window (a little JSON object). I have read some other SO posts about cross-domain request issues, but I don't understand the solutions, or what the difference is between sending the GET from my code or from the browser. Can someone explain why this behavior occurs, and what the appropriate change to my code/design is? My existing code is as below:
$.ajax({
type: "get",
url: auth_ad_act_url,
data: {
ads_token: ACCESS_TOKEN
},
dataType: 'jsonp',
success: function(data, status) {
console.log(data);
}
});
EDIT:
I do a jQuery.ajax() of type "get" supplied with a URL, parameters object, and success callback function, and dataType 'jsonp' to deal with cross-domain requesting.
I posted new code. Now the error is that the response is not correct. (I know this because it worked from my browser, and that responsee lined up with the API documentation). The response is "Resource interpreted as Script but transferred with MIME type text/html: https://host.com/apps/[my-app-id]/authorize_ad_account?callbac…" but it should be an object with the key 'url' and one other thing. I also get the error "Uncaught SyntaxError: Unexpected token :" when I include 'jsonp'. But that incorrect response mentioned above still gets logged to console so I don't understand when that syntax error happens, or where.
The Same Origin Policy does not include what you type into your address bar. If it did, you literally would not be able to access any website at all unless it was saved on your local machine!
In your situation, in order to get the resource that you need from jQuery's get, you'll either need to use a server-side proxy hosted on a matching domain, or since you're consuming JSON see if the API you're using supports JSONP.
There is a possibility to get JSON Data with a cross-domain request. You have to use JSONP and define a callback method, which has to be in the call and in the JSON Data.
Your request:
$.ajax({
type: "GET",
url: auth_ad_act_url + "&callback=?",
jsonpCallback: "jsonCallback",
dataType: "jsonp",
success: function(data) {
// Do something with the data
}
)};
The JSON File on the external server:
jsonCallback(INSERT_HERE_THE_JSON_DATA);
If you do not have the possibility to add the jsonCallback on the external server, check out CORS.

Simple jQuery $.get example code not working

I'm trying to get the output of a webpage and I found that jQuery has a method for doing this through the $.get method.
From the link above, I found the following example code:
$.get('ajax/test.html', function(data) {
$('.result').html(data);
alert('Load was performed.');
});
I modified the example code to the following:
$.get('http://www.google.com', function(data) {
$('.result').html(data);
alert('Load was performed.');
});
But it doesn't work. See it here: http://jsfiddle.net/6WQqD/1/
What am I doing wrong? How can I make a simple HTTP request that outputs the content of the page?
Same origin policy:
In computing, the same origin policy is an important security concept for a number of browser-side programming languages, such as JavaScript. The policy permits scripts running on pages originating from the same site to access each other's methods and properties with no specific restrictions, but prevents access to most methods and properties across pages on different sites.
For cross-domain request you can use one of the following approaches:
1 JSONP
It requires the server part supports jsonp (contains specific code for crossdomain requests).
For GET requests try use dataType = jsonp in ajax settings:
$.ajax({
url: "other-domain-url",
type: 'GET',
crossDomain: true,
dataType: 'jsonp',
jsonpCallback:'callback',
cache: false
});
You should specify callback function to be executing after the response is retrieved. Response will be passed as a parameter.
2 Use transparently proxy on the server part of your site. Configurate proxy (e.g. nginx) to redirect necessary local requests to other domain.
So in js you will use local ajax:
$.ajax({
url: "/local",
type: 'GET'
}
and in config you should use proxy settings:
location /local {
proxy_pass http://other-domain-url
}

Ajax Request Fails why?

following my code:
$('#btnOK').click(function(e){
e.preventDefault();
$form = $('#testForm');
dataString = $form.serialize();
$.ajax({
type: "POST",
url: 'https://abc.com/login',
data: dataString,
dataType: 'xml',
success: function( returnData ) {
alert(returnData);
}
});
});
My Code fails to make ajax request to http://abc.com/login page and doesn't gives out result. What's the mistake here?
Is it due to cross domain likely:
my domain: http://xyz.com
login domain: http://abc.com/login
unless your abc.com domain is configured to deal with cross-origin policy, you won't be able to make a cross-domain ajax call
A solution is to redirect your ajax call to a server side proxy under xyz.com domain who send your data and get the response from abc.com (e.g. using CURL).
or if you're in control of abc.com domain you could send proper headers (Access-Control-*) to allow crossdomain calls
my domain: http://xyz.com
login domain: http://abc.com/login
This is cross domain issue you can get over with
Proxy on your site
JsonP support from abc.com site
If you just want to post and dont care what result is, you can create a form and a iframe then set target of the form to that iframe, then just post (wont work with csrf enabled site)
Just debug it use chrome or ie debugger, make a breakpoint at the entry of the click() function, and then step by step, you'll find the problem.

Categories

Resources