Solved

pushing a json object to a restful service call in wcf

Posted on 2013-01-25
6
454 Views
Last Modified: 2013-01-25
I have the following json data object


[
    {
        "dob": "3/24/2012 12:00:00 AM",
        "first_name": "FirstName-8604     ",
        "id": 69,
        "last_name": "LastName-8604     ",
        "mrn": "0",
        "pat_seqno": 2121,
        "site_id": 12
    },
    {
        "dob": "1/6/2012 12:00:00 AM",
        "first_name": "FirstName-4     ",
        "id": 70,
        "last_name": "LastName-4     ",
        "mrn": "0",
        "pat_seqno": 2121,
        "site_id": 12
    },
    {
        "dob": "9/8/2012 12:00:00 AM",
        "first_name": "FirstName-840     ",
        "id": 71,
        "last_name": "LastName-840     ",
        "mrn": "0",
        "pat_seqno": 2123,
        "site_id": 12
    }]

Open in new window


And I to push it a wcf service that I am working and push it to a database table of some sort, this is how I have the method set up in the Ifile

[OperationContract]
        [WebInvoke(Method = "POST", BodyStyle=WebMessageBodyStyle.WrappedRequest, RequestFormat=WebMessageFormat.Json, ResponseFormat=WebMessageFormat.Json, UriTemplate="PutPatientData")]
        string SavePatientData(PatientRecords[] patrecs);

Open in new window


And right now I have some placeholder colder to make sure that the method is working for me.
        public string SavePatientData(PatientRecords[] patrecs)
        {
            string strReturn = "saved";

            return strReturn;
        }

Open in new window


how do I call this method with the above json object, so that I see it in the backend code and store it in the database.
0
Comment
Question by:JDEE8297
  • 3
  • 2
6 Comments
 
LVL 14

Expert Comment

by:binaryevo
ID: 38819356
Your going to have to up your points if anyone is going to respond... :=)  Just friendly advice...
0
 

Author Comment

by:JDEE8297
ID: 38819541
did that peek your interest @binaryevo :)
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 400 total points
ID: 38819618
Use :

var patient_records = [
    {
        "dob": "3/24/2012 12:00:00 AM",
        "first_name": "FirstName-8604     ",
        "id": 69,
        "last_name": "LastName-8604     ",
        "mrn": "0",
        "pat_seqno": 2121,
        "site_id": 12
    },
    {
        "dob": "1/6/2012 12:00:00 AM",
        "first_name": "FirstName-4     ",
        "id": 70,
        "last_name": "LastName-4     ",
        "mrn": "0",
        "pat_seqno": 2121,
        "site_id": 12
    },
    {
        "dob": "9/8/2012 12:00:00 AM",
        "first_name": "FirstName-840     ",
        "id": 71,
        "last_name": "LastName-840     ",
        "mrn": "0",
        "pat_seqno": 2123,
        "site_id": 12
    }];

        $.ajax({
            type: "POST",
            url: "yourpage.asmx/SavePatientData",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            data: JSON.stringify({ patrecs: patient_records }),
            success: function (msg) {
                alert("success : " + msg.d);
            },
            error: function (msg) {
                alert("failed :  " + msg.d);

            }
        });

Open in new window

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:JDEE8297
ID: 38819688
that worked for me, although I should have called this out in my original request, where I am  using a google chrome plugin Advanced Rest Client to do my testing on this and that is where I am having issues.
0
 
LVL 82

Expert Comment

by:leakim971
ID: 38819710
ok, so why don't we see that on your original question?
0
 

Author Comment

by:JDEE8297
ID: 38819774
As I forgot to call it out, my bad. Your solution worked for me and I will give you the points, but was just wondering if you had any idea on how I could use the plugin for chrome to do the testing with.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

932 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now