Solved

pushing a json object to a restful service call in wcf

Posted on 2013-01-25
6
455 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
This article discusses how to create an extensible mechanism for linked drop downs.
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…

813 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

11 Experts available now in Live!

Get 1:1 Help Now