JQuery Ajax request to http://pastebin.com/raw.php - javascript

I try to make a JQuery ajax request to 'http://pastebin.com/raw.php' and using this code:
$.ajax({
url: 'http://pastebin.com/raw.php',
data: "i=VJ29uFnk",
complete: function(jqXHR, textStatus) {
alert('complete');
},
success: function(data) {
alert(data);
},
error: function(xhr, status, error) {
alert('noh!')
}
});
With this I get a status '404' within xhr but the url I can see in firebug looks correct:
http://pastebin.com/raw.php?i=VJ29uFnk
Ideas?

XHR calls are protected under the Same origin policy.
What you can do, however, is call a server side script that bypass this.

You could circumvent the same origin policy by creating a php script that captured the data you want: example.com/getpage.php?url=pastebin.com/raw.php?i=VJ29uFnK.

Related

$.post not triggering function

I really don't get why alert or console.log is not being triggered in this code:
$.post("http://localhost:8080/mail",
jsonObject,
function(data) {
console.log("Done!");
alert("Thank you for your inquiry. We will get back to you soon.");
alert("Response: " + JSON.stringify(data));
}
);
While I can see the mail API works as I was able to get the email with the values I put in the HTML forms. alert and console.log is not being triggered what could be the reason?
I can see this on the browser log though:
Cross-Origin Request Blocked: The Same Origin Policy disallows reading
the remote resource at http://localhost:8080/mail. This can be fixed
by moving the resource to the same domain or enabling CORS.
Could this be the reason? If so what should I do to make $.post trigger success or failure.
Cross Origin Requests (CORS) is your issue. $.ajax() crossDomain parameter as documented here
If you wish to force a crossDomain request (such as JSONP) on the same domain, set the value of crossDomain to true. This allows, for example, server-side redirection to another domain.
$.ajax({
type: "POST",
url: "http://localhost:8080/mail",
crossDomain : true,
data: jsonObject
...
})
Regarding how to catch a success or failure: You can chain done(), fail(), and always() to $.post
$.post("http://localhost:8080/mail", jsonObject, function(data) {
// alert("success");
}).done(function(){
// alert("success 2");
}).fail(function() {
// alert( "error" );
}).always(function(){
// alert( "finished" );
});
Which also works with $.ajax() or use callbacks within $.ajax()
$.ajax(function(){
success: function (responseData, textStatus, jqXHR) {},
error: function (responseData, textStatus, errorThrown) {}
});

Detect HTTP 301 status code from JavaScript

I need to detect client side if a requested file (with XMLHttpRequest) had a 301 response. The reason of doing this is because I need to request other files related to the file where user has been redirected and not related to the first one requested.
Any way to detect this response status code using JavaScript or JQuery?
Thanks.
jQuery ajax callback function gives out a lot of info
$.ajax({
url: "test.php",
type: "GET",
data: {},
dataType:"json",
success: function(resp, textStatus, xhr) {
//status of request
console.log(xhr.status);
},
complete: function(xhr, textStatus) {
console.log(xhr.status);
}
});
You can use $.ajax with jquery
It has everything you need here : http://api.jquery.com/jQuery.ajax/
If you look at "statusCode" explaination, you will see you can make something for every code

jquery ajax does not complete

I'm having a trouble with ajax requests and server responses:
$.ajax({
url: servurl,
dataType: "jsonp",
data: {... },
crossDomain: true,
error: function(){},
success: function(){},
complete: function(){alert('complete')}
});
}
The thing is - sometimes I get succes, when I should get it, but sometimes I can get 500 status, and it is normal and expected.
The same ajax call works for correct requests, but fails for others.
I want to display an error message if I get a 500 server error, but for some reason the ajax does not complete. Thus, neither error: nor complete: work.
Maybe the reason for that is 'jsonp' datatype? Other datatypes do not work though.
Can someone help please?
Or maybe give me an advice on how to detect server status any other way.
jsonp requests do not trigger error callbacks by design, therefore there is no way for you to catch the error with javascript. I suggest instead implementing an error handler on your server that detects a jsonp request and returns jsonp that indicates an error has occured rather than a 500 status code.
Note that error: is deprecated as of 1.8 and is not called for JSONP however I wonder if you might have success using the Promise functionality introduced with 1.5 for deferred http://api.jquery.com/category/deferred-object/ as:
jqXHR.fail(function(jqXHR, textStatus, errorThrown) {});
jqXHR.done(function(data, textStatus, jqXHR) {});
jqXHR.always(function(data|jqXHR, textStatus, jqXHR|errorThrown) { });
Example for your code:
$.ajax({
url: servurl,
dataType: "jsonp",
data: {... },
crossDomain: true
}).done(function(data, textStatus, jqXHR){ //replace success
alert(textStatus);
}).always(function(data|jqXHR, textStatus, jqXHR|errorThrown) { // replace complete
alert(textStatus);
}).fail(function(jqXHR, textStatus, errorThrown) { // replace error
alert(errorThrown);
});
Make sure that you are accessing to your server. Maybe you are requesting in your server for an specific contentType (like application/json) and you are not using that property into your ajax call.
As you requested, to show any message if get a error (400, 404, 500...), you can use my custom function for ajax error responses:
function onErrorFunc(jqXHR, status, errorText) {
alert('Status code: ' + jqXHR.status + '\nStatus text: ' + status +
'\nError thrown: ' + errorText);
}
Usage:
$.ajax({
//some options
error: onErrorFunc
});
Please, show us what error thrown your server.
Thank you all for comments. Jquery .ajax really does not give errors on jsonp requests.
The way to get error messages was to implement the jquery-jsonp plugin:
https://github.com/jaubourg/jquery-jsonp

Retrieving user_timeline from Twitter in XML

I'm writing a simple app in HTML and Javascript. I'm trying to retrieve my user_timeline via jQuery's .ajax() method. The problem is that I want to retrieve my timeline in XML but I'm keep failing with this simple task. Here's my code:
$.ajax({
type: 'GET',
dataType: 'xml',
url: 'http://api.twitter.com/1/statuses/user_timeline.xml?screen_name=stepanheller',
success: function(data, textStatus, XMLHttpRequest) {
console.log(data);
},
error: function(req, textStatus, error) {
console.log('error: '+textStatus);
}
});
Weird thing is that when I try exactly the same thing but with JSON instead of XML then the script works.
$.ajax({
type: 'GET',
dataType: 'jsonp',
url: 'http://api.twitter.com/1/statuses/user_timeline.json?screen_name=stepanheller',
success: function(data, textStatus, XMLHttpRequest) {
console.log(data);
},
error: function(req, textStatus, error) {
console.log('error: '+textStatus);
}
});
Can you give me some hints what I'm doing wrong with the request? I know that I'm using old version of API but I won't deal with OAuth right now. Thanks.
It is generally impossible to send a Cross-domain ajax request. It's the general rule.
JsonP is the classic way to work around this limitation, and there is no equivalent for Xml according to my knowledge. Depending on your browser compatibility constraints, you can use XHR2 to achieve this.
Otherwise, the only solution is to set up a server proxy.
Client --Ajax--> Your server --HttpRequest--> Twitter

jQuery URL Get error on Firefox

Can anyone point out why my JavaScript function is falling into the error function rather than the success function? Firefox on Ubuntu
$(document).ready(function() {
console.log( "Start" );
$.ajax({ type: "GET", dataType: "html", url: "http://slashdot.org",
error: function(request, status) {
console.log("Error");
},
success: function(data) {
console.log("Sucess");
}
});
console.log( "End" );
});
Because of same-origin security restrictions, you cannot issue ajax calls to domains other than the domain of your current web page.
Possible work-arounds depending upon what your actual problem is:
Build a server proxy on your domain that will fetch the web page from the other site for you so you can send the request to your own domain.
Use an iframe to display the content from another domain.
It is very common issue with Cross Domain Policy. If you are using jQuery Ajax then you can use JSONP to do cross domain query. Document at http://api.jquery.com/jQuery.ajax/
$.ajax({ type: "GET", dataType: "json", url: "https://api.instagram.com/v1/tags/stackoverflow/media/recent?client_id=0324ff3396ef4ab49697505678e734f5&callback=?",
error: function(request, status) {
console.log(request, status);
},
success: function(data) {
console.log(data);
}
});

Categories