Click here to Skip to main content
15,885,309 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
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

<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
<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>


What I have tried:

i tried the above solutions but its not working when i am accessing same at local
Posted
Updated 17-May-16 5:14am
v3

1 solution

That is CORS related issue .these problem you might face when your WCF service is hosted in one domain and your client in another,There are several tricks are there to fix this issue,as i have worked in WEBAPI there are several document available in net by which i fixed the issue.
Please check the below link may be it will help something.

Enabling CORS in WCF[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900