[英]How to get value from jquery which come from api controller
Hello I have a web API controller which is returning an integer value and I don't know how to get it in a jquery ajax function. 您好,我有一个Web API控制器,它返回一个整数值,我不知道如何在jquery ajax函数中获取它。 My API controller is following.
我的API控制器正在关注。
[HttpGet]
[Route("api/tblProducts/{UserId}/CheckUserAvil")]
[ResponseType(typeof(int))]
public IHttpActionResult CheckUserAvil(string UserId)
{
int Exist = db.CheckUserIdAvil(UserId);
return Ok(Exist);
}
And my jquery code is given 和我的jQuery代码给出
$.getJSON("/api/tblProducts/" + Userid + "/CheckUserAvil", function (data) {
});
I don't know how to handle response I want Exist variable value which come from controller. 我不知道如何处理我想要的来自控制器的变量值。 Please help me.
请帮我。
UPDATE My Stored Procedure Code is fallowing 更新我的存储过程代码无效
USE [MakaAnOrderDB]
GO
/****** Object: StoredProcedure [dbo].[CheckUserIdAvil] Script Date: 8/19/2018 1:18:30 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[CheckUserIdAvil]
@UserId nvarchar(255)
AS
BEGIN
SET NOCOUNT ON;
DECLARE @Exists INT
IF EXISTS(SELECT @UserId
FROM tblUser
WHERE UserId = @UserId )
BEGIN
SET @Exists = 1
END
ELSE
BEGIN
SET @Exists = 0
END
RETURN @Exists
END
GO
And it is workin well in Sql But in Api.It is returing -1 all the time 它在Sql但在Api中很好用。它一直在-1重复
the callback argument data
will have the data you seek. 回调参数
data
将包含您要查找的数据。
One suggestion would be to have the action return a model that makes the returned data easier to understand. 一种建议是让操作返回一个模型,该模型使返回的数据更易于理解。
For example, the following uses an anonymous object to hold the data. 例如,以下使用匿名对象来保存数据。
[HttpGet]
[Route("api/tblProducts/{UserId}/CheckUserAvil")]
[ResponseType(typeof(int))]
public IHttpActionResult CheckUserAvil(string UserId) {
int exist = db.CheckUserIdAvil(UserId);
var model = new {
Exist = exist
};
return Ok(model);
}
the client will then extract the desired data from the response in the callback function 然后客户端将从回调函数中的响应中提取所需的数据
$.getJSON("/api/tblProducts/" + Userid + "/CheckUserAvil", function (data) {
var exist = data.Exist;
console.log("Exist: " + exist);
//...
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.