简体   繁体   English

使用Javascript(AJAX)向Symfony2中的路由发送请求

[英]Send a request to a route in Symfony2 using Javascript (AJAX)

I have a working prototype Symfony2 RESTful webservice which I am still working on and I am trying to figure out how a client can send JSON or consume JSON data from the webservice. 我有一个工作原型Symfony2 RESTful webservice,我仍在努力,我试图弄清楚客户端如何发送JSON或从Web服务使用JSON数据。 All I need is an example(s) on how to send a request or post data to it and I can figure out the rest. 我只需要一个关于如何发送请求或向其发布数据的示例,我可以弄清楚其余部分。 From my browser, if I visit http://localhost/app_dev.php/users.json , I get the correct result from my database as JSON, eg [{"id":1,"username":"paulo","username_canonical":"paulo","email":"a@ymail.com","email_canonical":"a@ymail.com","enabled":true,"salt":"66r01","password":"UCxSG2v5uddROA0Tbs3pHp7AZ3VMV","last_login":"2013-12-03T13:55:15-0500","locked":false,"expired":false,"roles":[],"credentials_expired":false,"first_name":"Monique","last_name":"Apple"}, ... etc. 从我的浏览器,如果我访问http://localhost/app_dev.php/users.json ,我从我的数据库获得正确的结果为JSON,例如[{“id”:1,“username”:“paulo”,“ username_canonical “:” 保罗 “ ”电子邮件“: ”a@ymail.com“, ”email_canonical“: ”a@ymail.com“, ”已启用“:真实的, ”盐“: ”66r01“, ”密码“:” UCxSG2v5uddROA0Tbs3pHp7AZ3VMV”, “LAST_LOGIN”: “2013-12-03T13:55:15-0500”, “锁定”:假的, “过期”:假的, “角色”:[], “credentials_expired”:假的, “FIRST_NAME”: “Monique”,“last_name”:“Apple”},......等

All other routes are working correctly and I can get the same result by using httpie or cURL. 所有其他路由都正常工作,我可以使用httpie或cURL获得相同的结果。 Now, the problem I am trying to solve is to get the same JSON data using AJAX (and mobile iOS, Android, etc later). 现在,我想解决的问题是使用AJAX(以及后来的移动iOS,Android等)获取相同的JSON数据。 Here is my attempt at using AJAX JS: 这是我尝试使用AJAX JS:

<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
<script type="text/javascript">


        $.ajax({
            dataType: "json",
            type: "GET",
            url: "http://192.168.1.40/symfony/web/app_dev.php/users.json",
            success: function (responseText)
            {
                alert("Request was successful, data received: " + responseText); 
            },
            error: function (error) {
                alert(JSON.stringify(error));
            }
        });
</script>

The AJAX alerts the following results which indicates an error: {"readyState":0,"responseText":"","status":0,"statusText":"error"} AJAX警告以下结果,表示错误:{“readyState”:0,“responseText”:“”,“status”:0,“statusText”:“error”}

What am I doing wrongly and how can I solve the problem. 我做错了什么,如何解决问题。 Kindly give an example. 请举个例子。

This is a cross-domain request issue. 这是跨域请求问题。 You need to make the request to the same domain you are on. 您需要将请求发送到您所在的域。 This is a browser-level security feature. 这是一个浏览器级别的安全功能。

For example, you can only request URLs on http://192.168.1.40 if you are currently ON http://192.168.1.40 . 例如,如果您当前正在http://192.168.1.40 ,则只能在http://192.168.1.40上请求URL。 This means that a request from http://192.168.1.39 (for example) won't work 这意味着来自http://192.168.1.39 (例如)的请求将不起作用

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

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