Perform GET request using jquery - javascript

I need to perform the following GET request,
telnet somesite.com 80
GET /index.html HTTP/1.0
using javascript, jQuery.
I've tried to follow the instructions in this site in particular the following code:
$.ajax({
url: 'http://somesite.com',
success:function(data){
alert(data);
}
});
but It doesn't work!
Where am I wrong?

Try this one:
$.ajax({
type: "GET",
url: "http://somesite.com",
timeout: 300000,
contentType: "application/json; charset=utf-8",
success: success,
error: failure
});
function failure(response) {
alert(response);
}
function success(response) {
alert(response);
}

by your code im assuming you are doing a cross domain ajax request. Which are automatically blocked by the browser.
you can either use the allow domain header using Cors see this Cross Domain Get Request in JS/JQuery
or switch to JSONP

If you want to perform a cross domain request try this
Working DEMO
You can use this in your head tag
<script src="https://rawgithub.com/IonicaBizau/jQuery-cross-domain-requests/master/js/jquery.xdomainajax.js">
</script>
code
$.ajax({
url: 'http://somsite.com', // Or your web page link
type: 'GET',
success: function(res) {
alert(res);
}
});

Related

Firefox extension missing Referer url in ajax header when making ajax calls

I am trying to make an ajax call to a server and the server needs referer url to identify my request
$.ajax({
url: abc + '/123/xyz/',
cache: false,
type: 'GET',
crossDomain: true,
headers: {
"key": "xxxxxxxxxxx"
}
}).done(function(result) {
executeOnSuccess();
});
The expected result on the server should be that if i execute this line request.getHeader("referer");
I should get the referer url but i get a null, but if i make the same ajax request using chrome extension it works and i get the referer url.
I have been stuck on this for a while now. The other option is to add referer url manually to the ajax header but i was expecting it to work like chrome?
Does any one has any idea about it ?
Thanks in advance.
The browser will always overwrite the referrer. Meaning you can't change the referrer of an ajax call. But you can try!
$.ajax({
url: abc + '/123/xyz/',
type: "GET",
headers: {
"Referer": "Change here reference"
},
success: function (data) {
alert("Success");
},
error: function (data) {
console.log(data);
}
});
Also, note that if you are planning to submit your extension in AMO, you should always use https in your calls to any server.

ajax https request jsonp success callback not working

Im run this script on my http site, on network tab show what request 200 ok,
but success callback not runing
$.ajax({
url: 'https://seobudget.ru',
success: function(data){
alert("123")
},
crossDomain:true,
dataType:"jsonp"
});
im try remove "crossDomain:true" but its nothing change
$.ajax({
url: 'https://seobudget.ru',
dataType:"jsonp",
success: function(data){
alert("123")
}
});
you put dataType in wrong place

Cross domain ajax causing issues to render data

I trying to capture data from an HTML form which will be placed on another website. From that form I need to capture data into my website. But when I tried jQuery Ajax call for cross domain it shows me 302 error with no response.
I've tried this
$('button[type="button"]').on('click', function(){
var data = $('.data-capture-form').serialize();
$.ajax({
type : 'POST',
url: 'http://prospectbank.co.uk/leads/test',
dataType: 'jsonp',
crossDomain : true,
data : data,
contentType: 'application/jsonp'
}).done(function(res){
var resp = $.parseJSON(res);
console.log(resp);
});
});
Where is problem with this code? Any help?
Fiddle Code
If you have access to the server, add the following header:
Access-Control-Allow-Origin: *
And then make JSONP calls
$.ajax({
type: "POST",
dataType: 'jsonp',
...... etc ....
If you do not have access to the external server, you have to direct the request to your server and then make a proxied call to the external server.

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".

jquery ajax returns status code 0 while requesting cross domain

i am trying to make cross domain request but server can not response with
Access-Control-Allow-Origin: *
i am trying to read response header.
when i am trying to read status code or ready state it returns 0. my code for ajax request is as follows.
$.ajax({
url: "https://accounts.google.com/o/oauth2/auth",
type: "POST",
data: {"response_type":"token","client_id":"6720XXXXXXX.apps.googleusercontent.com","redirect_uri":"http://localhost:51967/oauth2callback.aspx","scope":"https://www.googleapis.com/auth/calendar","state":"this is state information","approval_prompt":"auto"},
crossDomain: true
}).always(function(jqxhr,testStatus,errorThrown)
{
alert(jqxhr.readyState);
alert(jqxhr.status);
});
thanks in advance
have you try this jquery crossdomain plugin?
another way is using jsonp as response type
$.ajax({
url: "https://accounts.google.com/o/oauth2/auth",
type: "POST",
dataType: 'jsonp',
...

Categories

Resources