Ajax jquery making web api call
Ajax jquery making web api call
我用java编写了一个api,它允许用户获取数据。有一个电话:..../api/users返回所有可用用户的列表
现在我有一个网站的搜索用户按钮,当你按下这个按钮,我想让一个调用/api/用户与Ajax的帮助。
我得到的部分,你可以点击搜索按钮,但我不明白如何使调用ajax
这是我的代码:
$.ajax({
url: ”api / resource / users ",
dataType: "json”,
}
).fail(
funcNon(jqXHR, textStatus) {
alert("APIRequestfailed: " + textStatus);
}
).done(
funcNon(data) {
alert("succes!")
}
);
这是使用ajax进行良好调用的方式吗?或者我必须使用:
http://localhost/projectUser/api/resource/users ?
假设您正在使用JQuery进行Ajax调用,那么这个示例代码应该对您有所帮助。它的作用是;
- 点击搜索按钮
- 做AJAX调用从你的Java REST API获取东西
- 当预期的JSON对象返回时,解析它并做一些事情
$(document).ready(function() {
$('#demoSearchBtn').click(function () {
// Search button was clicked!
$.ajax({
type: "GET",
url: "http://localhost/projectUser/api/resource/users", // edit this URL to point into the URL of your API
contentType: 'application/json; charset=utf-8',
dataType: "json",
success: function (data) {
var jsonObj = $.parseJSON(data);
// Do something with your JSON return object
},
error: function (xhr) {
alert('oops something went wrong! Error:' + JSON.stringify(xhr));
}
});
});
}
如果这个http://localhost/projectUser/api/resource/users
是url,它要么是
$.ajax({
url: ”api/resource/users", ...
或
$.ajax({
url: ”http://localhost/projectUser/api/resource/users", ...
取决于浏览器的当前URL(相对或绝对取决于浏览器的上下文)。
,但它是永远不会 ”api / resource / users "
,单词和斜杠之间有空格。
相关文章:
- 如何更改bigquery API中的计费层选项
- Amazon S3 REST API大小不正确
- 客户端服务器REST API captcha实现
- 使用Facebook live API创建实时视频对象时的隐私设置
- 谷歌放置API:按国家或餐馆名称搜索餐馆
- MeteorJS:在带有回调的vzaar api上正确使用wrapAsync
- 是否可以控制获取哪些Google地图脚本(JavaScript API)
- API密钥使用和检查示例
- 谷歌地图API v3不适用于移动浏览器或PhoneGap
- 任何方式使AJAX调用Gmail API,而无需通过JS库
- 谷歌地图JS API+JSON-多个标记没有显示
- Slack Oauth/Authorize API Call
- jQuery append with api call
- Ajax Call To Web API
- Rest api get call using jquery
- YouTube JavaScript Player API seekTo() call
- Flickr API call IIFE
- Ajax jquery making web api call
- Call Windows API from node.js msg
- Call API from Rails