AJAX和jQuery的问题

<html> 
<head> 
<title>Ajax</title> 
<script type="text/JavaScript" src="jquery-1.5.1.min.js"></script> 
<script type="text/JavaScript"> 

function register()
{ 
    $.ajax({
    type: "GET",
    url: "http://exampleurl.com/api/index.php",
    data: "action=login_user&app_key=MyAPIKey&username=Bob&password=HisPassword",
    dataType: "text",
    success: function(data) 
         {
                    alert(data);
         }
    error: function (jqXHR, textStatus, errorThrown) 
         {
                    alert(errorThrown);
         }
          });
}
</script> 

</head> 
<body> 
<form>      
<input type="button" value="Test" onclick="register()"/>
</form>
</body> 
</html>

The URL returns a string (plain-text) when used in the address bar. Why is the above code not working? When I click on the button, I get no alert. Basically, nothing happens.

Browsers tried: IE 8 and Chrome.

Thank you for your time.

There's probably an exception being thrown in the $.ajax call.

Consider

  1. ensuring your URI is well formed by taking the query parameters out of the URL and supplying a data object instead:

    data: { action: login_user, app_key: .... etc }

  2. adding an error handler too

Is example.com the URL of your local site? If not, your above example violates Same Origin Policy. You can circumvent this by doing the call through a proxy on the server side.

Try to add

error: function (jqXHR, textStatus, errorThrown) {
    alert(errorThrown);
}

http://api.jquery.com/jQuery.ajax/

Have you tried to separate the data ?

$.ajax({
    type: "GET",
    url: "http://exampleurl.com/api/index.php",
    data: "action=login_user&app_key=MyAPIKey&username=Bob&password=BobPassword",
    dataType: "text",
    success: function(data) {
        alert(data);
    }
});

Do you have access over the backend url?

You might need to declare the contentType. You might also need to do a post and set the data.

data: "{ 'action': 'login_user', 'app_key': 'MyAppKey', etc. }"