Solved

Really simple WCF Service using JQuery keep getting error 415 unsupported media type

Posted on 2008-10-15
1
4,996 Views
Last Modified: 2012-05-05
I wrote a very simple WCF service with one operation contract that returns a list of points. I then wanted to use JQuery to consume the service and get the list of points on to a web page.  I was following Rick Strahl's blog: http://www.west-wind.com/weblog/posts/324917.aspx.

I keep geeting the error: 415 unsupported media type

Please could you let me know where I'm going wrong. Thanks!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.ServiceModel.Web;
 
namespace Contracts
{
    [ServiceContract(Name = "TestService", Namespace = "Contracts")]
    public interface ITestService
    {
        [OperationContract]
        [WebInvoke(
            Method = "POST", 
            BodyStyle = WebMessageBodyStyle.Wrapped, 
            ResponseFormat = WebMessageFormat.Json)]
        List<Point> Search();
    }
}
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
 
namespace Contracts
{
    [DataContract]
    public class Point
    {
        [DataMember]
        public double X { get; set; }
        [DataMember]
        public double Y { get; set; }
    }
}
 
    <script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
    <script type="text/javascript" src="http://www.json.org/json2.js"></script>
 
        function Run() {
            var url = "http://localhost:1436/services/testservice.svc/Search";
            var json = JSON.stringify("");
            
            $.ajax({
                url: url,
                data: json,
                type: "POST",
                processData: false,
                contentType: "application/json",
                timeout: 10000,
                dataType: "text",
                success: function(result) { alert("Success"); },
                error: function(result) { alert("Error"); }
            });           
        }

Open in new window

0
Comment
Question by:dnoxs
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 2

Accepted Solution

by:
dnoxs earned 0 total points
ID: 22721626
Figured it out. I had the incorrect web config it should have looked as follows:
	<system.serviceModel>
		<behaviors>
			<endpointBehaviors>
				<behavior name="WebBehavior">
					<webHttp />
				</behavior>
			</endpointBehaviors>			
			<serviceBehaviors>
				<behavior name="Portal.Services.TestServiceBehavior">
					<serviceMetadata httpGetEnabled="true" httpGetUrl="" />
					<serviceDebug includeExceptionDetailInFaults="false" />
				</behavior>
			</serviceBehaviors>
		</behaviors>
		<services>
			<service behaviorConfiguration="Portal.Services.TestServiceBehavior"
			 name="Portal.Services.TestService">
				<endpoint address="" binding="webHttpBinding" behaviorConfiguration="WebBehavior" contract="Contracts.ITestService">
					<identity>
						<dns value="localhost" />
					</identity>
				</endpoint>
				<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
			</service>
		</services>
	</system.serviceModel>	

Open in new window

0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question