Click here to Skip to main content
12,633,985 members (28,048 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: Ajax REST WebService
Hi everyone

I am trying to call the Rest services using Ajax Call with Method = Post; My Ajax code is below. In my alert i am getting my results added. But in the Html form i was not able to add this using AJAX.
$.ajax({
                    type: "POST", //GET or POST or PUT or DELETE verb
                    url: "http://localhost:8080/karthick", // Location of the service
	            data:'{"name":"karthi","firstName":"Peter"}',
                    contentType: "application/json", // content type sent to server
                    dataType: "json", //Expected data format from server
                    processdata: true, //True or False
                    success: function (json)
                    var result = json.firstName;
                    $("#dvAjax").html(result);
                    },
                    error: ServiceFailed
                });


                return false;
            });



        });

function ServiceFailed(xhr) {
            alert(xhr.responseText);
            if (xhr.responseText) {
                var err = xhr.responseText;
                if (err)
                    error(err);
                else
                    error({ Message: "Unknown server error." })
            }
            return;
        }

HTML CODE
 <fieldset>
                               
                                <table id="karthi">
                                  
                                    <tr>
                                        <td><label>Name</label></td>
                                        <td align="left"><input type="text" id="name" class="medium" name="" value=""></td>
                                        
                                    </tr>
                                    
                                    <tr>
                                        <td><label>firstName</label></td>
                                        <td align="left"><input type="text" id="firstname" class="medium" name="" value=""></td>
                                        
                                    </tr>
                                    
                                   
                                   

                                   

                                    <tr>
                                        <td colspan="2"><button id="btnAjax" onClick="submit" type="button">Submit</button> or <a href="#">Cancel</a></td>
                                        <td></td>
                                        <td></td>
                                        <td></td>

                                    </tr>

                                </table>


                            </fieldset>
                               


I want to add the name & firstname parameter in the HTML form using the ajax call with the web-service. Kindly assist me on this.

Regards
Karthick
Posted 25-Feb-13 1:04am
Updated 25-Feb-13 8:32am
v3

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

try like this
function GetSearchResult() {
    $("#loading").show();
    $.support.cors = true;
    var searchUrl = URL;
    var params = { "ID": Id,
        "Name": name
        
    };
    $.ajax({
        cache: false,
        type: "POST",
        async: true,
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        url: searchUrl,
        data: JSON.stringify(params),
        success: function (data) {
           
        },
        error: function (xhr) {
            alert(xhr.responseText);
        }
    });
}


Hope this helps
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.161208.2 | Last Updated 25 Feb 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100