Click here to Skip to main content
Rate this: bad
good
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
Edited 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
0 OriginalGriff 6,055
1 DamithSL 4,621
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,500
4 Sergey Alexandrovich Kryukov 3,294


Advertise | Privacy | Mobile
Web03 | 2.8.141220.1 | Last Updated 25 Feb 2013
Copyright © CodeProject, 1999-2014
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