1

I have code to check username and password in test.php file.

But when I am calling this ajax it is always showing alert of wrong username and password.
Can anyone tell where I am going wrong?

$.ajax({
    type: "GET",
    url: 'http://externalurl/external/test.php',
    contentType: "text/html",
    data: 'uname=' + uname + '&pass=' + pass,
    success: function (data) {
        if (data == 'success') {
            alert('success');
        } else {
            alert('Wrong user name and password.Please try again');
        }
    }
});
ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
J.K.A.
  • 7,272
  • 25
  • 94
  • 163

1 Answers1

1
$.ajax({
    type: "GET",
    url: 'test.php',
dataType: 'jsonp',
    contentType: "text/html",
    crossDomain:'true',
    data: {uname: "admin", pass: "admin"},
    success: function (json) {
        //process the json here.
    }
});

You are using incorrect format for data field of ajax .

And no, Javascript doesn't normally allow you to access data via ajax from external servers.It will give out

Origin http://externalhost is not allowed by Access-Control-Allow-Origin.

Edit:

However, You can set crossDomain and dataType:'jsonp' for getting JSON data from an external server.

NT_
  • 2,216
  • 1
  • 18
  • 23
  • now it showing response in firebugs net window but not alerting it – J.K.A. May 19 '12 at 11:33
  • do `alert(data);` . ie, `success: function (json) { alert(json) }` – NT_ May 19 '12 at 11:34
  • @Kunal !! what do you mean by you already did. What's it alerting?. It wont alert anything unless you return JSON data from the PHP . – NT_ May 19 '12 at 11:38
  • means I already tried this code : `alert(data); . ie, success: function (json) { alert(json) }` but still alert not showing the response is coming into the firebugs net window i.e. yes and no – J.K.A. May 19 '12 at 11:41
  • that is because Javascript doesnt allow you to access NON JSON data from external server. Aren't you reading at all?. Respond from the server with JSON. – NT_ May 19 '12 at 11:42