简体   繁体   English

如何从 webmethod 中的 function 返回 id 到 javascript 代码

[英]How to return id from function in webmethod to javascript code

I have this javascript function to insert data using JSON我有这个 javascript function 使用 JSON 插入数据

function Create(Name,Description) {
    var obj = {};
    obj.Name = Name;
    obj.Description = Description;
    $.ajax
        ({
            type: "Post",
            url: "/test.asmx/Create",
            dataType: 'JSON',
            contentType: "application/Json; Charset= Utf-8",
            data: JSON.stringify(obj),
            success: function () {
                alert("Great");
            },
            error: function (response) {
                alert(response);
            }
        });
}

And this my WebMethod in my web service I need to get the id of data inserted in my javascript code这是我的 web 服务中的WebMethod我需要获取插入到我的 javascript 代码中的数据的 ID

[WebMethod]
public int Create(string Name, string Description)
{
    try
    {
        var Qer = new DAL.MOD.TABLE1
        {
            kName = Name,
            Description = Description
        };
        t.Create(Qer);
        return Qer.Id;
    }
    catch
    {
        return -1;
    }
}

Does anyone have any a suggestion how to achieve this?有没有人有任何建议如何实现这一目标?

If ajax call and the web method works fine then you can access the result through success function of the ajax code;如果 ajax 调用并且 web 方法工作正常,那么您可以通过 Z2705A83A39A265 的 function 代码成功访问结果;

 success: function (result) {
                                alert(result);
                            },

In the above snippet, result parameter should have the Id value which is returned from the web method.在上面的代码片段中,结果参数应该具有从 web 方法返回的 Id 值。

You can access the Id in the ajax success function.您可以访问 ajax 中的 Id成功function。 If it goes into error function then you need to check your service.如果它进入错误 function 那么你需要检查你的服务。

 function Create(Name,Description) { var obj = {}; obj.Name = Name; obj.Description = Description; $.ajax ({ type: "Post", url: "/test.asmx/Create", dataType: 'JSON', contentType: "application/Json; Charset= Utf-8", data: JSON.stringify(obj), success: function (response) { if (response) { // Query Id alert(response); } }, error: function (response) { alert(response); } }); }

function Create(Name,Description) {
                var obj = {};
                obj.Name = Name;
                obj.Description = Description;
                $.ajax
                    ({
                        type: "Post",
                        url: "/test.asmx/Create",
                        dataType: 'JSON',
                        contentType: "application/Json; Charset= Utf-8",
                        data: JSON.stringify(obj),
                        success: function (response) {
                            alert(response.d);
                        },
                        error: function (response) {
                            alert(response);
                        }
                    });
            }

Ok, as others noted, you need to create/setup/have a function that gets called back when the async call is done.好的,正如其他人指出的那样,您需要创建/设置/拥有一个 function ,它会在异步调用完成时被回调。 You can use the function() in-line and that works just fine.您可以使用内联函数()并且效果很好。 So a WHOLE new function() is created on the fly.所以一个全新的 function() 是动态创建的。

Next up?下一个? You can't just use "response", but have to use ".d" for data.您不能只使用“响应”,而必须使用“.d”作为数据。

Thus this will work:因此这将起作用:

   $.ajax({  
        type: "POST",  
        url: 'WebForm1.aspx/HelloWorld',
        contentType: "application/json",  
        datatype: "json",  
        success: function(responseFromServer) {  
            alert(responseFromServer.d)  
        }  
    });  
}  

Note the.d to get data out of the response object.注意.d 从响应 object 中获取数据。

And if you return two values?如果你返回两个值? Say this:说这个:

[WebMethod()]
public static string[] HelloWorld()
{
string[] twovalues = new string[2];
twovalues[0] = "This is the first return value";
twovalues[1] = "This is the second return value";

return twovalues;

} }

So, now you can get at the two return values like this:因此,现在您可以像这样获得两个返回值:

   $.ajax({  
        type: "POST",  
        url: 'WebForm1.aspx/HelloWorld',
        contentType: "application/json",  
        datatype: "text",  
        success: function(responseFromServer) {  
            alert(responseFromServer.d[0]);
            alert(responseFromServer.d[1]);
        }  
    });  

So the response is a object returned.所以响应是返回的 object。 You use the ".d" method of the response object to get at the data.您使用响应 object 的“.d”方法获取数据。 And as per above, if more then one value is returned, then you can use the [] array reference to pluck out/grab each value returned.并且如上所述,如果返回一个以上的值,那么您可以使用 [] 数组引用来提取/获取每个返回的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM