Go Premium for a chance to win a PS4. Enter to Win


Posting from Jquery to .Nt web service for insert

Posted on 2013-10-28
Medium Priority
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 vb.net

The jQuery will AJAX POST to an asp.net 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 60

Accepted Solution

Julian Hansen earned 1000 total points
ID: 39605921
The jQuery will AJAX POST to an asp.net 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

ID: 39608303
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 1000 total points
ID: 40145056
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

ID: 40379838
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 60

Expert Comment

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

Featured Post

Threat Trends for MSPs to Watch

See the findings.
Despite its humble beginnings, phishing has come a long way since those first crudely constructed emails. Today, phishing sites can appear and disappear in the length of a coffee break, and it takes more than a little know-how to keep your clients secure.

Question has a verified solution.

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

Online collaboration is quickly becoming embedded in the workplace, and its benefits are tangible. See what the current landscape looks like and what the future holds for collaboration tools and the future of work.
In order to have all security and back ups taken care of, WordPress users can sign up for services with WP Engine.
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 how to dynamically set the form action using jQuery.
Suggested Courses

971 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