Posting from Jquery to .Nt web service for insert

Posted on 2013-10-28
Last Modified: 2014-10-15
I have a scenario where I have a HTML/JQuery quiz application that needs to post behind the scenes to an ASP.Net page that will insert a record into a database using the data passed to it from jQuery.

Please can someone confirm or correct my understanding and provide examples of the below in

The jQuery will AJAX POST to an web service, rather than a page?
The data will need to be passed to the web servie as JSON?
After insert the web service will need to return some sor tof JSON back so the JQuery can carry on?

Many thanks
Question by:swgdesign
LVL 51

Accepted Solution

Julian Hansen earned 250 total points
Comment Utility
The jQuery will AJAX POST to an web service, rather than a page?
There is no distinction between these. There is a URL that data is posted to which optionally returns data. The implementation of this is irrelevant.

The data will need to be passed to the web servie as JSON?
It doesn't have to be - in fact if you post it - by default it will come through as a standard query string containing key value pairs example: ?id=1&name=fred ...

After insert the web service will need to return some sor tof JSON back so the JQuery can carry on
Again not necessarily - it depends on your application. If your application requires a response from the AJAX call then it can handle this in the Success function - if not it can post the data and carry on without waiting for a response.

Data does not have to be returned as JSON - this is an option (commonly used) but you can return the data in any form you wish. The decision is based on how the client will want to deal with the returned information.

AJAX is just a way to send and receive data between client and server asynchronously - the format you want to send and receive data in is determined by your application requirements.

What would you expect back from the post after submitting quiz data?

Author Comment

Comment Utility
I haven't really done much web service stuff like this so excuse my lack of knowledge and thanks.

All I need to return to the JQuery quiz is a whether the passed querystring data was inserted into the db, so that it can move on to the next question.

Any chance of pointing me to some code similar to what you have said.

Assisted Solution

smeghammer earned 250 total points
Comment Utility
I assume you already have a webservice endpoint that will accept and process the POST request?


You have a HTML form with - lets say - one text field:

<input type="text" id="quizThing" value="" />

Open in new window

You need to refer to this to retrieve the value - e.g. using JQuery:

var thingToSubmit = $("#quizThing").val();

Open in new window

You then meed to pass this asynchronously, again using JQuery ajax. Here you have a bunch of callbacks you can use if you wish, particularly if there is a return value:

  url: "/your/net/endpoint",
  .success(function( data) {
    NOTE - your server function might complete successfully, but fail to insert into the DB. You must cater for this...
    alert(data.ok);  //e.g. true or false, assuming JSON object is returned.
       //move your quiz on to next question...
  alert("something went wrong...");

Open in new window

I have not tested this, but the basic principle is to send data to the end point and process the result - success or failure.

LVL 58

Expert Comment

Comment Utility
I've requested that this question be closed as follows:

Accepted answer: 500 points for smeghammer's comment #a40145056
Assisted answer: 0 points for swgdesign's comment #a39608303

for the following reason:

This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
LVL 51

Expert Comment

by:Julian Hansen
Comment Utility
Typo in admin closing - should be "split points between smeghammer and julianh"

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
DLL in ASP.NET 20 38
Visual Studio: built-in keystroke automation 2 20
C3-->D3 Line Chart 4 16
Need sample Angular apps for study 4 30
In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
The purpose of this video is to demonstrate how to set up the WordPress backend so that each page automatically generates a Mailchimp signup form in the sidebar. This will be demonstrated using a Windows 8 PC. Tools Used are Photoshop, Awesome…
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…

728 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

13 Experts available now in Live!

Get 1:1 Help Now