使用crossdomain时,在ajax上访问WCF时不允许使用405方法。 [英] 405 method not allowed while accessing WCF on ajax while using crossdomain.

查看:59
本文介绍了使用crossdomain时,在ajax上访问WCF时不允许使用405方法。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在asp.net中为手机间隙应用程序创建了一个wcf服务,我将在phonegap中使用ajax访问这些方法..在工作的方法链接



但是当我在本地使用相同的方法时,它会给出405错误:



请求网址:http://www.halawiat.com/Services/Service.svc/GetCustomers

请求方法:选项

状态代码:405方法不允许

远程地址:192.169.245.150:80

响应标题

查看源

Access-Control-Allow-Headers:Content -Type,接受

访问控制 - 允许 - 方法:POST,GET,OPTIONS

访问控制 - 允许 - 原点:*

Access-Control-Max-Age:1728000

允许:POST,LOCK,UNLOCK

缓存控制:私有

内容长度:1565

内容类型:text / html; charset = UTF-8

日期:2016年4月26日星期二11:48:21 GMT

服务器:Microsoft-IIS / 8.5

X -AspNet-版本:4.0.30319

X-Powered-By:ASP.NET

X-Powered-By-Plesk:PleskWin

请求标题

查看源

接受:* / *

接受编码:gzip,deflate,sdch

Accept-Language:en-US,en; q = 0.8

Access-Control-Request-Headers:accept,content-type

Access-Control-Request-Method: POST

连接:keep-alive

主持人:www.halawiat.com

来源:http:// localhost:1497

Referer:http:// localhost:1497 / CS / Default.aspx

User-Agent:Mozilla / 5.0(Windows NT 6.1)AppleWebKit / 537.36(KHTML,与Gecko一样)Chrome / 50.0.2661.87 Safari / 537.36







这是我的代码



i created a wcf service in asp.net for phone gap application where i will access those method using ajax in phonegap.. method working on Working Link

but when i am tring same method locally it giver 405 error:

Request URL:http://www.halawiat.com/Services/Service.svc/GetCustomers
Request Method:OPTIONS
Status Code:405 Method Not Allowed
Remote Address:192.169.245.150:80
Response Headers
view source
Access-Control-Allow-Headers:Content-Type, Accept
Access-Control-Allow-Methods:POST,GET,OPTIONS
Access-Control-Allow-Origin:*
Access-Control-Max-Age:1728000
Allow:POST, LOCK, UNLOCK
Cache-Control:private
Content-Length:1565
Content-Type:text/html; charset=UTF-8
Date:Tue, 26 Apr 2016 11:48:21 GMT
Server:Microsoft-IIS/8.5
X-AspNet-Version:4.0.30319
X-Powered-By:ASP.NET
X-Powered-By-Plesk:PleskWin
Request Headers
view source
Accept:*/*
Accept-Encoding:gzip, deflate, sdch
Accept-Language:en-US,en;q=0.8
Access-Control-Request-Headers:accept, content-type
Access-Control-Request-Method:POST
Connection:keep-alive
Host:www.halawiat.com
Origin:http://localhost:1497
Referer:http://localhost:1497/CS/Default.aspx
User-Agent:Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.87 Safari/537.36



here is my code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.ServiceModel.Web;

[ServiceContract]

public interface IService
{
	[OperationContract]
    [WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
    string GetCustomers(string prefix);

    [OperationContract]
    [WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
    string ExecuteProcedure(object JsonObject);
}







service.cs




service.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Data.SqlClient;
using System.Configuration;
using System.Web.Script.Serialization;
using System.ServiceModel.Activation;
using System.Data;

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Service : IService
{
	public string GetCustomers(string prefix)
	{
            Dictionary<string, KeyValuePair<SqlDbType, string>> Parameters = new Dictionary<string, KeyValuePair<SqlDbType, string>>();
            string str = DataAccess.GetJsonFromCommandTextp("select * from customers", Parameters).ToString();
            return str;
    }





web.config





web.config

<br />
<blockquote class="quote"><div class="op">Quote:</div><configuration><br />
 <system.webServer><br />
       <modules runAllManagedModulesForAllRequests="true" /><br />
   <br />
    <httpProtocol><br />
      <customHeaders><br />
        <add name="Access-Control-Allow-Origin" value="*"/><br />
        <add name="Access-Control-Allow-Headers" value="Content-Type, Accept" /><br />
        <add name="Access-Control-Allow-Methods" value="POST,GET,OPTIONS" /><br />
        <add name="Access-Control-Max-Age" value="1728000" /><br />
      </customHeaders><br />
    </httpProtocol><br />
  </system.webServer><br />
<br />
<br />
  <appSettings/><br />
  <connectionStrings><br />
    <add name="constr" connectionString="Data Source = ; Initial Catalog = phonegap; Integrated Security = true"/><br />
  <add name="ResourcesConnectionString" connectionString="Data Source = ; Initial Catalog = phonegap; Integrated Security = true"/><br />
  </connectionStrings><br />
<br />
  <system.web><br />
 <customErrors mode="Off"/><br />
<br />
    <!-- <br />
            Visual Basic options:<br />
            Set strict="true" to disallow all data type conversions <br />
            where data loss can occur. <br />
            Set explicit="true" to force declaration of all variables.<br />
        --><br />
    <compilation debug="true"  targetFramework="4.5"/><br />
   <br />
    <authentication mode="Windows"/><br />
   <br />
    <pages controlRenderingCompatibilityVersion="4.5" clientIDMode="AutoID"><br />
      <namespaces><br />
        <clear/><br />
        <add namespace="System"/><br />
        <add namespace="System.Collections"/><br />
        <add namespace="System.Collections.Generic"/><br />
        <add namespace="System.Collections.Specialized"/><br />
        <add namespace="System.Configuration"/><br />
        <add namespace="System.Text"/><br />
        <add namespace="System.Text.RegularExpressions"/><br />
        <add namespace="System.Linq"/><br />
        <add namespace="System.Xml.Linq"/><br />
        <add namespace="System.Web"/><br />
        <add namespace="System.Web.Caching"/><br />
        <add namespace="System.Web.SessionState"/><br />
        <add namespace="System.Web.Security"/><br />
        <add namespace="System.Web.Profile"/><br />
        <add namespace="System.Web.UI"/><br />
        <add namespace="System.Web.UI.WebControls"/><br />
        <add namespace="System.Web.UI.WebControls.WebParts"/><br />
        <add namespace="System.Web.UI.HtmlControls"/><br />
      </namespaces><br />
    </pages><br />
    <httpHandlers><br />
      <br />
    </httpHandlers><br />
   <br />
  </system.web><br />
  <!-- <br />
        The system.webServer section is required for running ASP.NET AJAX under Internet<br />
        Information Services 7.0.  It is not necessary for previous version of IIS.<br />
    --><br />
  <system.serviceModel><br />
<br />
      <br />
    <behaviors><br />
      <serviceBehaviors><br />
        <behavior name="ServiceBehavior"><br />
          <serviceMetadata httpGetEnabled="true"/><br />
          <serviceDebug includeExceptionDetailInFaults="true"/><br />
        </behavior><br />
      </serviceBehaviors><br />
      <endpointBehaviors><br />
        <behavior name="ServiceAspNetAjaxBehavior"><br />
          <enableWebScript/><br />
        </behavior><br />
      </endpointBehaviors><br />
    </behaviors><br />
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/><br />
    <standardEndpoints><br />
 <webScriptEndpoint><br />
 <standardEndpoint name="" crossDomainScriptAccessEnabled="true" /><br />
 </webScriptEndpoint><br />
 </standardEndpoints><br />
 <br />
    <services><br />
      <service behaviorConfiguration="ServiceBehavior" name="Service"><br />
        <endpoint address="" binding="webHttpBinding" contract="IService" behaviorConfiguration="ServiceAspNetAjaxBehavior"><br />
          <br />
        </endpoint><br />
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/><br />
      </service><br />
    </services><br />
  </system.serviceModel><br />
<br />
</configuration></blockquote><br />





ajax call



ajax call

<blockquote class="quote"><div class="op">Quote:</div>$.ajax({<br />
                type: "POST",<br />
                contentType: "application/json; charset=utf-8",<br />
                url: "http://www.halawiat.com/Services/Service.svc/GetCustomers",<br />
                crossDomain: true,<br />
                data: '{"prefix": "aaa"}',<br />
                processData: false,<br />
                dataType: "json",<br />
                success: function (response) {<br />
                    var customers = eval(response.d);<br />
                    var html = "";<br />
                    $.each(customers, function () {<br />
                        html += "<span>Name: " + this.CustomerName + " Id: " + this.CustomerId + "</span><br />";<br />
                    });<br />
                    $("#results").html(html == "" ? "No results" : html);<br />
                },<br />
                error: function (a, b, c) {<br />
                    alert(a.responseText);<br />
                }<br />
            });</blockquote>





我的尝试:



i尝试了上述解决方案,但是当我在本地访问时,它无法工作



What I have tried:

i tried the above solutions but its not working when i am accessing same at local

推荐答案

.ajax({< br />
类型:POST,< br />
contentType:application / json; charset = utf-8,< br />
url:http://www.halawiat.com/Services/Service.svc/GetCustomers\",<br />
crossDomain:true ,< br />
数据:'{前缀:aaa}',< br />
processData:false,< br />
dataType:json ,< br />
成功:函数(响应){< br />
var customers = eval(response.d);< br />
var html = ;< br />
.ajax({<br /> type: "POST",<br /> contentType: "application/json; charset=utf-8",<br /> url: "http://www.halawiat.com/Services/Service.svc/GetCustomers",<br /> crossDomain: true,<br /> data: '{"prefix": "aaa"}',<br /> processData: false,<br /> dataType: "json",<br /> success: function (response) {<br /> var customers = eval(response.d);<br /> var html = "";<br />


.each(customers,function(){< br />
html + =< span> Name:+ this。客户名称+Id:+ this.CustomerId +< / span>< br />;< br />
});< br />
.each(customers, function () {<br /> html += "<span>Name: " + this.CustomerName + " Id: " + this.CustomerId + "</span><br />";<br /> });<br />


(#results)。html(html ==?无结果:html);< br />
},< br />
错误:function( a,b,c){< br />
alert(a.responseText);< br />
}< br />
});< / blockquote>
("#results").html(html == "" ? "No results" : html);<br /> },<br /> error: function (a, b, c) {<br /> alert(a.responseText);<br /> }<br /> });</blockquote>





我有什么尝试过:



i尝试了上述解决方案,但是当我在本地访问时它不起作用



What I have tried:

i tried the above solutions but its not working when i am accessing same at local


这篇关于使用crossdomain时,在ajax上访问WCF时不允许使用405方法。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆