从sinatra应用程序向Rails应用程序发出发布请求

问题描述:

我需要从sinatra应用程序向rails应用程序发送POST请求,该请求将返回一些json.我在本地测试此功能.网址如下:

I need to send a POST request from a sinatra app to a rails app which would return back some json. Im testing this functionality locally. The urls are as follows:

Rails app : railsapp.mydomain.com/api/v1.json
Sinatra app: sinatraapp.mydomain.com

在localhost上,URL为:

On localhost, the urls are:

Rails app: localhost:3000/api/v1.json
Sinatra app:localhost 3001

在本地运行的sinatra应用程序中,我具有以下代码以在本地发出POST请求

In my sinatra app running locally, i have the following code to make the POST request locally

$("#submit").click(function(){
   $.post("http://localhost:3000/api/v1.json",
     {email:"<email_here>",password:"<password_here>"},
     function(data) {
         //Do something with response
     }
   );

});

此外,请求标头中的Content-Type应该为"application/x-www-form-urlencoded".我在Firefox中使用REST Client来测试请求,该请求可以正常工作,但是在上面的代码中根本没有发出请求.我的代码有什么错误?

Also, the Content-Type in the request header should be "application/x-www-form-urlencoded". I used REST Client in Firefox to test the request and it works, but in the above code the request is not being made at all. What is the error in my code ?

谢谢

此作为XSS攻击而被阻止.即使它们在同一个域中,子域也不同,这就足够了.有关更多信息,请参见> AJAX调用是子域被视为跨站点脚本?.

This is being stopped as a XSS attack. Even though they are on the same domain, the sub-domains are different, and that's enough. For more information, see Are AJAX calls to a sub-domain considered Cross Site Scripting?.

要纠正此问题,您可以简单地使AJAX命中本地控制器,然后使用ruby发出请求,而不受上述限制的限制.

To correct this, you could simply make the AJAX hit your local controller, and make the request using ruby, which would not be limited by said restriction.