【问题标题】:Ajax Post call to the Rest Web Services from browser not working due to CORS由于 CORS,从浏览器对 Rest Web 服务的 Ajax Post 调用无法正常工作
【发布时间】:2016-05-07 03:06:41
【问题描述】:

click here for error screenshot

我正在尝试通过浏览器的 Web 服务将数据插入数据库,但出现错误(屏幕截图)。

   $("#signup").click(function () {
            var radio = $('input[name="radio"]:checked').val();
            var fullname = $("#fullname").val();
            var email = $("#email").val();
            var phoneno = $("#phoneno").val();
            var password = $("#password").val();
            var dataString = "{'Cust_Type':'" + radio + "','Contact_Name':'" + fullname + "','Contact_No':'" + phoneno + "','Email':'" + email + "','Password':'" + password + "'}";

            if ($.trim(radio).length > 0 & $.trim(fullname).length > 0 & $.trim(phoneno).length > 0 & $.trim(email).length > 0 & $.trim(password).length > 0) {
                $.post({
                    type: "POST",
                    url: "http://test.com/testWebservices/testservices.svc/mobilesignup",
                     dataType: "json",
                    data: dataString,
                    crossDomain: true,
                    cache: false,
                    beforeSend: function () { $("#signup").val('Connecting...'); },
                    success: function (data) {
                        if (data == "success") {
                            alert("Thank you for Registering with us! you can login now");
                        }
                        else if (data = "exist") {
                            alert("Hey! You alreay has account! you can login with us");
                        }
                        else if (data = "failed") {
                            alert("Something Went wrong");
                        }
                    }
                });
            } return false;

        });

我正在使用 phonegap 从 broswer 调用服务 api(不工作),但是当我尝试从邮递员插件调用时,它工作并且数据已成功插入数据库。

【问题讨论】:

  • 您的 js 文件是否托管在 Web 服务器(例如 IIS)上?
  • 不。我正在使用 phonegap 创建 apk 文件..
  • 网站 test.com 托管在哪个网络服务器中?
  • iis server.only 托管服务。
  • 在真实设备上尝试,而不是在浏览器上。

标签: android ajax cordova cors phonegap-plugins


【解决方案1】:

如果 Test.com 网站托管在 IIS7

在应用程序的根目录(http://test.com)创建Web.config文件,并在其中添加以下XML代码

<?xml version="1.0" encoding="utf-8"?>
<configuration>
 <system.webServer>
   <httpProtocol>
     <customHeaders>
       <add name="Access-Control-Allow-Origin" value="*" />
     </customHeaders>
   </httpProtocol>
 </system.webServer>
</configuration>

更新:

对于 IIS6

  1. 打开 Internet 信息服务 (IIS) 管理器。
  2. 右键单击要为其启用 CORS 的站点 (test.com) 并转到 属性。
  3. 切换到 HTTP 标头选项卡。
  4. 在“自定义 HTTP 标头”部分中,单击“添加”。
  5. 输入Access-Control-Allow-Origin 作为标题名称。
  6. 输入* 作为标头值。
  7. 点击确定。

【讨论】:

  • 我正在使用 IIS 6。但我已经在我的 web.config 中使用了此代码。
  • 收到此错误“跨域请求被阻止:同源策略不允许读取位于test.com/testWebservices/testService.svc/mobilesignup 的远程资源。(原因:CORS 标头“Access-Control-Allow-Origin”缺失)”
  • “请求的资源上不存在‘Access-Control-Allow-Origin’标头。因此,源‘null’不允许访问。响应包含Chrome的HTTP状态代码”
  • 当我将此行放入 web.config 时,我得到 500 internal server error.and 网站没有打开..
  • 请继续执行 IIS6 的步骤并重新启动 IIS 服务器。
猜你喜欢
  • 2013-01-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-24
  • 2021-07-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多