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');
        }
    }
});
16
  • 8
    "where I am going wrong?" => you're sending a password with GET. Commented May 19, 2012 at 11:01
  • check the net console in Firebug or Chrome's dev tool and see what's the actual response. Also, what James said Commented May 19, 2012 at 11:01
  • 3
    And you are not using an object for data which will cause problems on certain characters. Use data: {uname: uname, pass: pass} instead! Commented May 19, 2012 at 11:02
  • Call external url through $.ajax in wordpres theme link Commented May 19, 2012 at 11:04
  • @ThiefMaster : I used this data: {uname: uname, pass: pass} but still same problem Commented May 19, 2012 at 11:07

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

now it showing response in firebugs net window but not alerting it
do alert(data); . ie, success: function (json) { alert(json) }
@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 .
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
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.