简体   繁体   English

使用ajax jquery从远程服务器访问php

[英]accessing php from remote server using ajax jquery

I am currently using phonegap to make an apps. 我目前正在使用phonegap制作应用。 As we know, phonegap only allow html css and js to be used. 众所周知,phonegap仅允许使用HTML CSS和JS。 therefore, php cannot be used. 因此,不能使用php。 I have researched a bit and I found out that I can just put the php file in a remote servers and used the ajax to call for the php file using the url of the remote server. 我进行了一些研究,发现可以将php文件放在远程服务器中,并使用ajax使用远程服务器的url来调用php文件。 however, it still cannot call the php file. 但是,它仍然无法调用php文件。

here is my ajax snippet of code in the login.html: 这是我的login.html中的Ajax代码段:

$(document).ready(function(){
    $("#btn").click(function(){
        var username = $("#username").val();
        var password = $("#password").val();
        var dataString = 'name1=' + username + '&password1='+ password;
        var theUrl =  'auth.php';
        if(username==''||password==''){
            alert("Please Fill All Fields");
        } else {
            $.ajax({
                type: "POST",
                url: theUrl,
                data: dataString,
                cache: false,
                success: function(result){
                    if (result == "right") {
                        window.location.href = 'home.html';
                    } else {
                        alert("Wrong username or password");
                    }
                }
            });
        }
        return false;
    });
});

As I am testing using auth.php which is still in the same folder as my login.html, everything works fine. 当我使用仍与我的login.html放在同一文件夹中的auth.php测试时,一切正常。 but when I use the url of the file in the remote server, it always fails. 但是当我在远程服务器中使用文件的url时,它总是失败。

Thanks in advance for responding. 预先感谢您的回复。

You have two options 你有两个选择

1. Using localhost 1. 使用本地主机

try this url 试试这个网址

theUrl=http://10.0.2.2/yourfilepath but this will work only in emulator not in browser .If you want test browser disable the web security first and then try it. theUrl=http://10.0.2.2/yourfilepath但这仅适用于模拟器而不是浏览器 。如果要测试浏览器,请先禁用Web安全性,然后再尝试。

2. Using liveserver 2. 使用liveserver

Access the files like a website url access eg: www.apptest.in/test/auth.php 像访问网站网址一样访问文件,例如: www.apptest.in/test/auth.php

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

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