繁体   English   中英

使用ajax / jquery从客户端网站传递参数到webservice方法

[英]From client website pass parameters to webservice method using ajax/jquery

我有两个网站

  1. SyncTest网站
  2. RemoteWebsite(客户端网站)

SysteTestWebsite包含具有以下方法的Web服务“ Service.svc”

[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]<br>
public class Service
{

    [OperationContract]
    public void DoWork()
    {
        // Add your operation implementation here
        return;
    }

    [OperationContract]
    [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json)]
    public string SyncJson(string p1)
    {
        return p1;
    }

    // Add more operations here and mark them with [OperationContract]
}

我想从RemoteWebsite的HTML页面访问此方法,以下是此HTML页面的代码。

    function SyncJson() {
        var sPhone = "test data";
        var data = { p1: sPhone };
        data = JSON.stringify(data);
        $.ajax({
            type: "POST",
            contentType: "application/json;",
            url: "http://mydomain:12887/Service.svc/SyncJson",
            data: data,
            dataType: 'json',
            success: function () {
                alert('success');
            },
            error: function (msg) {
                console.log(msg);
                alert(msg.d);
            }
        });
    }


<p>
    Remote site
    <input type="button" onclick="SyncJson()" value="Send Test Data" />

</p>

但是我得到了这个错误:

NetworkError:405不允许的方法- http://abc.com/Service.svc/SyncJson

这是跨域请求。 创建服务时应考虑这一点。 了解有关CORS的更多信息。

暂无
暂无

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

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