简体   繁体   English

如何在AJAX中调用外部PHP文件

[英]How to call external PHP file in AJAX

I have code to check username and password in test.php file. 我有代码来检查test.php文件中的用户名和密码。

But when I am calling this ajax it is always showing alert of wrong username and password. 但是,当我调用此ajax时,它总是显示错误的用户名和密码的警报。
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');
        }
    }
});
$.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 . 您为ajax的data字段使用了不正确的格式。

And no, Javascript doesn't normally allow you to access data via ajax from external servers.It will give out 不,Javascript通常不允许您通过ajax从外部服务器访问数据,它将给出

Origin http:// externalhost is not allowed by Access-Control-Allow-Origin. Access-Control-Allow-Origin不允许使用原始http:// externalhost

Edit: 编辑:

However, You can set crossDomain and dataType:'jsonp' for getting JSON data from an external server. 但是,您可以设置crossDomaindataType:'jsonp'以便从外部服务器获取JSON数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM