AJAX呼叫休息服务 [英] AJAX Call On Rest Services

查看:124
本文介绍了AJAX呼叫休息服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好



我试图使用带有Method = Post的Ajax Call来调用Rest服务;我的Ajax代码如下。在我的警报中,我得到了我的结果。但是在Html表单中,我无法使用AJAX添加它。

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


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>





我想添加名称&使用带有Web服务的ajax调用的HTML表单中的firstname参数。请帮助我。



问候

Karthick



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

推荐答案

.ajax({
键入:POST,// GET或POST或PUT或DELETE动词
url:http:// localhost:8080 / karthick,//服务的位置
数据:''{ name:karthi,firstName:Peter}'',
contentType:application / json,//发送到服务器的内容类型
dataType:json,//服务器的预期数据格式
processdata:true,// True或False
成功:函数(json)
var result = json.firstName;
.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(结果);
},
错误:ServiceFailed
});


返回false;
});



});

函数ServiceFailed(xhr){
alert(xhr.responseText);
if(xhr.responseText){
var err = xhr.responseText;
if(err)
error(err);
else
error({消息:未知服务器错误。})
}
返回;
}
("#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


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>





我想添加名称&使用带有Web服务的ajax调用的HTML表单中的firstname参数。请帮助我。



问候

Karthick



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


试试这样

try like this
function GetSearchResult() {


这篇关于AJAX呼叫休息服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆