-
Jul 3rd, 2012, 07:51 AM
#1
Thread Starter
Hyperactive Member
WCF - JSON - JQuery
Hi,
FYI: My WCF are quite limited and did not do anything like the following:
I need to create a wcf service that clients can communicate with sending / receiving JSON objects.
The eventual client is going to be mobile apps (developed by another company) so while developing I figured I'll "consume" the service using jQuery in a MVC app.
Figured I'll simplify my problem where FireBug keep telling me I have an "Invalid Label" in my result ( {"Id":199,"Name":"Krokonoster"} )
Here's some code for what it might help to tell me what I'm doing wrong:
Code:
$('#getPerson').click(function (event) {
event.preventDefault();
$.ajax({
dataType: 'jsonp',
contentType: "application/json",
type: "GET",
url: "http://localhost:8732/service1/data/" + $("#personId").val(),
success: function (data) {
alert(data.Name);
}
});
});
Code:
[ServiceContract]
public interface IService1
{
[OperationContract]
Person GetData(string id);
}
Code:
public class Service1 : IService1
{
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Json,
UriTemplate = "data/{id}")]
public Person GetData(string id)
{
return new Person()
{
Id = Convert.ToInt32(id),
Name = "Krokonoster"
};
}
}
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
}
Code:
<?xml version="1.0"?>
<configuration>
<system.serviceModel>
<services>
<service name="app.service.Service1">
<endpoint address="http://localhost:8732/service1"
binding="webHttpBinding"
contract="app.service.IService1"/>
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior>
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
</configuration>
I guess this is just the beginning of my troubles since I still have to get to posting full JSON objects to my service, etc.
Guess going over a good book will help?
Got these (any recommendations? time's a limited)
- Microsoft Windows Communication Foundation 4.0 Cookbook for Developing SOA Applications
- Pro WCF 4
- WCF 4 Step by Step
-
Jul 3rd, 2012, 07:55 AM
#2
Re: WCF - JSON - JQuery
Well - that is valid JSON
{"Id":199,"Name":"Krokonoster"}
http://jsonlint.com/ will check your JSON for you.
Who is giving you this error message????
-
Jul 3rd, 2012, 07:58 AM
#3
Thread Starter
Hyperactive Member
Re: WCF - JSON - JQuery
FireBug....
That alert inside my $.ajax function does not fire at all....
-
Jul 3rd, 2012, 08:09 AM
#4
Re: WCF - JSON - JQuery
That ajax call never completes means the server is giving you the error - right?
Does that mean you see it in the CONSOLE tab of FIREBUG as the message from the server to the GET?
Also - why a GET and not a POST? I write my own web methods in ASP.Net and always use POST to return the JSON.
Code:
function submitAjax(strService, objWebParam, sender, fncFinished) {
objWebParam.newkeys = objWebParam.newkeys || [];
objWebParam.updkeys = objWebParam.updkeys || [];
objWebParam.editkey = objWebParam.editkey || "";
objWebParam.addkey = objWebParam.addkey || "";
objWebParam.username = window.username || "";
//objWebParam.objReturn = {};
//objWebParam.objReturn["bldg"] = "30";
var objSubmit = {};
mergeObjects(objSubmit, objWebParam);
clearWebParam(objSubmit);
var strWebParam = $.toJSON(objSubmit);
$.ajax({
type: "POST",
url: "WebService.asmx/" + strService,
dataType: "json",
data: strWebParam,
contentType: "application/json; charset=utf-8",
success: function(msg) {
fncFinished(msg, sender, objWebParam);
},
failure: function(msg) {
fncFinished(msg, "failure", objWebParam);
},
error: function(msg) {
fncFinished(msg, "error", objWebParam);
}
});
}
-
Jul 3rd, 2012, 08:13 AM
#5
Thread Starter
Hyperactive Member
Re: WCF - JSON - JQuery
Okey dokey, I'll give it a shot.
I pretty much started rewriting some service the guy before me did not finish (it was in vb.net and did not use JSON at all) so in many places did the same he allready done (from there the "GET"'s)
Thanks for the replies dude!
-
Jul 3rd, 2012, 09:52 AM
#6
Thread Starter
Hyperactive Member
Re: WCF - JSON - JQuery
This perhaps rings a bell?
"NetworkError: 405 Method Not Allowed - http://localhost:8732/service1/GetAnyString"
trying your method and seems to work....FireBug show this error though.
-
Jul 3rd, 2012, 09:56 AM
#7
Re: WCF - JSON - JQuery
Seems you have to tell your service to expect the POST request
http://social.msdn.microsoft.com/For...-73b7e7588e6d/
I just googled for
405 Method Not Allowed
My web config looks like this
Code:
<httpHandlers>
<remove verb="*" path="*.asmx"/>
<add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" validate="false"/>
</httpHandlers>
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|