Click here to Skip to main content
15,503,579 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Updated 25-Feb-13 8:32am
v3

1 solution

try like this
C#
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
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900