Solved

Posting from Jquery to .Nt web service for insert

Posted on 2013-10-28
6
253 Views
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
Simon
0
Comment
Question by:swgdesign
6 Comments
 
LVL 56

Accepted Solution

by:
Julian Hansen earned 250 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?
0
 

Author Comment

by:swgdesign
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.
0
 
LVL 4

Assisted Solution

by:smeghammer
smeghammer earned 250 total points
ID: 40145056
I assume you already have a webservice endpoint that will accept and process the POST request?

OK...

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:

$.ajax({
  url: "/your/net/endpoint",
  data:thingToSubmit,
type:"POST"
})
  .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.
    if(data.ok){
       //move your quiz on to next question...
    }
  })
.fail(function(){
  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.

Cheers
0
 
LVL 58

Expert Comment

by:Gary
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.
0
 
LVL 56

Expert Comment

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

Featured Post

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
The viewer will learn how to dynamically set the form action using jQuery.
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…

685 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