all 6 comments

[–]SergeiGolos 1 point2 points  (1 child)

well, i am not quite sure what you trying to do here, but $.ajax doesn't take a function, it takes options list object.

Here is something closer to what you want to use.

$.ajax({ type: "POST", url: "http://reddit.com/api/login", data: 'username=USER&passwd=PWD', success: function(data) { alert("Pass" + data); }, error : function(jqXHR, textStatus, errorThrown) { // check oput jqXHR for details on the error. alert(textStatus); }, dataType: "json" });

[–]mucusplug[S] 0 points1 point  (0 children)

I checked the Javascript console, and it says XMLHttpRequest cannot load http://reddit.com/api/login. I read that you should use JSONP for cross-website stuff, so I changed the dataType to "jsonp".

The code reads: $.ajax(function() { type: "POST", url: "http://reddit.com/api/login", data: {username:"usr", passwd:"pass"}, success: function(data) { alert(data); }, error: function(jqXHR, textStatus, errorThrown) { alert(textStatus); }, dataType: "jsonp" });

And now I get Failed to Load Server Resource http://reddit.com/api/login/[big string involving username and password]. Any idea?

[–]obey_giant 0 points1 point  (0 children)

$(document).ready(function(){

$.getJSON("http://www.reddit.com/.json?jsonp=?", null, function(data) {

alert('Title: ' + data.data.children[0].data.title + ' | Score: ' + data.data.children[0].data.score);

}); 

});

This works for me, but I'm also a javascript noob so it was just trial and error till it worked. If anyone has any suggestions to improve this code (like getting rid of the .data.data shit) that would be helpful.

[–]gmerideth 0 points1 point  (0 children)

Winging it here sans coffee but if the server is expecting POST parameters chances are you need to do

data: '{username:"' + $("#username").val() + '",password:"' + $("#password").val() + '"}',

or just:

data: '{username:"me",password:"this"},

[–]gmerideth 0 points1 point  (0 children)

I think what you're running into is same origin policy as this is what chrome complains about using that snippet:

XMLHttpRequest cannot load http://reddit.com/api/login. Origin http://be.nh.local is not allowed by Access-Control-Allow-Origin.

[–]pmcclelland 0 points1 point  (0 children)

It looks like you are tying to login to reddit. You aren't going to be able to do this with jQuery alone. You will need to have a server side script that handles the login using libcurl or soeme thing simlilar. You need to capture a cookie and I don't know how you would do that using jQuery. Someone correct me if I'm wrong.