简体   繁体   English

jQuery中的跨域URL

[英]Cross Domain URL in jQuery

I have made a very simple index.php code as following 我做了一个非常简单的index.php代码,如下所示

<!DOCTYPE html> 
<head> 
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/httpGet.js"></script>
<script type="text/javascript" src="js/jquery.mobile.js"></script>   

</head> 

<body>
 <input id="string" type="text"/>
 <input id="button" type= "button" value="Check" />

</body>
</html>

In this HTML code we get a number fro user and call httpgGet.js file. 在此HTML代码中,我们为用户获取了一个号码,并调用了httpgGet.js文件。 And the httpGet.js is : 而httpGet.js是:

$('#button').click(function() {
        var string = $('#string').val();
    $.post('http://...../isprime.php',{number: string},function(data){
      alert(data);
            } 
     }

we send the user number to a isprime.php file where it checks if a user number is a prime number or not. 我们将用户号发送到isprime.php文件,在其中检查用户号是否为素数。 When I say $.post('php/isprime.php',.....) it works very well, but when I want to call the isprime.php from another computer, it does not work. 当我说$ .post('php / isprime.php',.....)时,它工作得很好,但是当我想从另一台计算机上调用isprime.php时,它不起作用。

I mean $.post('192.168.1.1/test/isprime.php'....) gives me a not found error. 我的意思是$ .post('192.168.1.1/test/isprime.php'....)给了我一个未发现的错误。

The file exists and I am able to run the code using direct call from browser. 该文件存在,我能够使用浏览器中的直接调用来运行代码。 When I type in browser 192.168.1.1/test/isprime.php the file is reachable, but from the js file, it fails. 当我在浏览器中输入192.168.1.1/test/isprime.php时,该文件可以访问,但是从js文件中访问失败。 Obviously, I can ping 192.168.1.1 too. 显然,我也可以ping 192.168.1.1。 I checked the code even with FireBug and no way thorough. 即使使用FireBug,我也检查了代码,但没有彻底检查。

I searched a lot and came a cross that Browsers do not allow cross domain call (for security purpose, I guess), but I cannot find a solution to get rid of this problem. 我进行了很多搜索,发现一个十字架,浏览器不允许跨域调用(出于安全目的,我想),但是我找不到解决这个问题的解决方案。 There are something about YQL (which is a query language and I don't know if I can use it or not). 关于YQL(这是一种查询语言,我不知道是否可以使用它)。 Also I saw something about cULR but I am not sure if that works too. 我也看到了一些关于cULR的信息,但是我不确定这是否也可行。

Kindly help. 请帮助。 I really need it. 我真的很需要 thank u very much. 十分感谢。

The most obvious solution, in my opinion, would be to make the request on the server side where you do not have the cross-domain request restrictions using the cURL library (as you mentioned, yes it would work). 在我看来,最明显的解决方案是使用cURL库在没有跨域请求限制的服务器端发出请求(如您所述,是的,它可以工作)。 Of course, you would need to have the cURL library installed on your server. 当然,您需要在服务器上安装cURL库。 The PHP cURL manual should have enough information to get you started. PHP cURL手册应该具有足够的信息来帮助您入门。

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

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