【问题标题】:Sends Data to WCF Webservice link from AJAx从 AJAx 向 WCF Webservice 链接发送数据
【发布时间】:2017-01-28 05:35:41
【问题描述】:

希望将数据从 Ajax 发送到包含方法 GetQuickQuote(string x) 并从 Web 服务返回数据的 Wcf 服务。

jQuery

 $('#txtBox').blur(function () {
        debugger;
        $.ajax({
            type: "POST",
            url: "http://logicalfire-pc:8090/Libertytest.Service1.svc" + "/GetQuickQuote", 
            crossDomain: true,
            data: JSON.stringify({ x: 'ght'}),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (response) {
                if (response.d == true) {

                    alert("You will now be redirected.");
                }

            },
            error:function(eror)
            {
                alert('failure');
            }

        })
    });

WCF 服务包含

public string GetQuickQuote(string x)
        {
            var ReadXmlPath = GetApplicationPath() + "TextFile1.txt";
x=ReadXmlPath;
    return x;


       }

我得到以下回应:

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://logicalfire-pc:8090/Libertytest.Service1.svc/GetQuickQuote. (Reason: CORS header 'Access-Control-Allow-Origin' missing).

【问题讨论】:

    标签: c# jquery ajax wcf


    【解决方案1】:

    在您的global.asax webservice 文件中添加以下代码

    protected void Application_BeginRequest(object sender, EventArgs e)
    {
       HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
       if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
       {
          HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST");
          HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept");
          HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");
          HttpContext.Current.Response.End();
       }
    }
    

    【讨论】:

      猜你喜欢
      • 2018-08-11
      • 1970-01-01
      • 2014-01-03
      • 1970-01-01
      • 2013-05-02
      • 2012-10-03
      • 2014-10-12
      • 1970-01-01
      • 2014-10-17
      相关资源
      最近更新 更多