简体   繁体   English

从sinatra应用程序发送帖子请求到rails应用程序

[英]Making a post request from a sinatra app to a rails app

I need to send a POST request from a sinatra app to a rails app which would return back some json. 我需要从sinatra应用程序发送一个POST请求到一个rails应用程序,它将返回一些json。 Im testing this functionality locally. 我在本地测试这个功能。 The urls are as follows: 网址如下:

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

On localhost, the urls are: 在localhost上,网址是:

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

In my sinatra app running locally, i have the following code to make the POST request locally 在我本地运行的sinatra应用程序中,我有以下代码在本地发出POST请求

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

});

Also, the Content-Type in the request header should be "application/x-www-form-urlencoded" . 此外,请求标头中的Content-Type应为"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. 我在Firefox中使用REST Client来测试请求并且它可以工作,但是在上面的代码中根本没有提出请求。 What is the error in my code ? 我的代码中有什么错误?

Thank You 谢谢

You can also set this settings: 您还可以设置此设置:

jQuery.support.cors = true;

This enables you to do cross domain calls with jQuery. 这使您可以使用jQuery进行跨域调用。 It's probably not the best solution, since you are adding a vulnerability by using the following request header: Access-Control-Allow-Origin. 它可能不是最佳解决方案,因为您使用以下请求标头添加漏洞:Access-Control-Allow-Origin。

This is being stopped as a XSS attack. 这是作为XSS攻击而停止的。 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调用是否被视为跨站点脚本? .

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. 要纠正这个问题,你可以简单地让AJAX命中你的本地控制器,并使用ruby发出请求,这不受上述限制的限制。

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

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