通过服务器端UserGUID到客户方Ajax调用 [英] Pass Server side UserGUID to a Clientside Ajax call
本文介绍了通过服务器端UserGUID到客户方Ajax调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要通过用户的GUID给Ajax功能,但是,当我尝试我收到提示
语法错误:标识符数字文字后立即启动
块引用>我Serverside集团code
公众的Guid GetUserID()
{
GUID currentUserId =(GUID)Membership.GetUser()ProviderUserKey。 返回currentUserId;
}客户端code部分
<脚本>
$(函数(){
LoadUserUploadFiles();
}); ID =下;%= GetUserID()%>中;
功能LoadUserUploadFiles(ID){ 警报(ID);
VAR URL ='<%= RESOLVEURL(/ WebMethods.aspx / GetIndividuallyUploadedFiles)%>'; $阿贾克斯({
网址:网址,
键入:POST,
数据:JSON.stringify({ID:ID})
数据类型:JSON
的contentType:应用/ JSON的;字符集= UTF-8,
成功:函数(结果){ VAR HTML ='<表类=表悬停>';
HTML + ='<&THEAD GT;';
HTML + ='<第i个用户名和LT; /第i';
HTML + ='<第i上传日期< /第i';
HTML + ='<第i下载< /第i';
HTML + ='<第i< /第i';
HTML + ='< / THEAD>';
HTML + ='<&TBODY GT;';
$。每个(Result.d,功能(键,值){
HTML + ='< TR>';
HTML + ='< TD>' + value.UserName +'< / TD>';
HTML + ='< TD>' + value.DateStr +'< / TD>';
HTML + ='< TD>' +'&下; A HREF ='+ value.FilePath +>下载及下; / A>' +'< / TD>';
HTML + ='< TD>' +'<输入类型=按钮值=删除的onclick =deleteUploadFile(这一点,'+ value.Id +')>' +'< / TD>';
HTML + ='< / TR>'; });
HTML + ='< / TBODY>';
HTML + ='< /表>'; $(#uploaddata)HTML(HTML);
},
错误:功能(E,X){
警报(x.ResponseText);
}
});
}
解决方案在函数签名,你应该有参数名称。当调用该函数,你应该通过GUID来的功能。请参见下面的例子:
MARKUP:
<%@页面语言=C#AutoEventWireup =真codeFILE =Default.aspx.cs继承=_默认%GT;<!DOCTYPE HTML>< HTML的xmlns =http://www.w3.org/1999/xhtml>
<头=服务器>
<标题>< /标题>
< /头>
<身体GT;
<表ID =form1的=服务器>
< DIV>
<脚本>
//通知ID是参数的名称在这里
功能LoadUserUploadFiles(ID){
警报(ID); };
< / SCRIPT>
<输入ID =测试类型=按钮值=测试的onclick ='LoadUserUploadFiles(<%= GetUserID()%GT;)'/>
< / DIV>
< /表及GT;
< /身体GT;
< / HTML>code:
使用系统;
公共部分类_Default:System.Web.UI.Page
{
保护无效的Page_Load(对象发件人,EventArgs的发送)
{ } 公众的Guid GetUserID()
{
GUID currentUserId =(GUID)Membership.GetUser()ProviderUserKey。
返回currentUserId; }
}编辑:
您可以更改code是这样的:<脚本>
$(函数(){
LoadUserUploadFiles('<%= GetUserID()%GT;');
功能LoadUserUploadFiles(ID){
警报(ID);
VAR URL ='<%= RESOLVEURL(/ WebMethods.aspx / GetIndividuallyUploadedFiles)%>'; $阿贾克斯({
//将code的其余部分则在这里I need to pass User Guid to the Ajax function,But when i tried i'm getting error
SyntaxError: identifier starts immediately after numeric literal
My Serverside Code
public Guid GetUserID() { Guid currentUserId = (Guid)Membership.GetUser().ProviderUserKey; return currentUserId; }
Part of Client Side Code
<script> $(function () { LoadUserUploadFiles(); }); id = "<%=GetUserID()%>"; function LoadUserUploadFiles(id) { alert(id); var url = '<%= ResolveUrl("/WebMethods.aspx/GetIndividuallyUploadedFiles") %>'; $.ajax({ url: url, type: "POST", data: JSON.stringify({ id: id }), dataType: "json", contentType: "application/json; charset=utf-8", success: function (Result) { var html = '<table class="table-hover">'; html += '<thead>'; html += '<th>Username</th>'; html += '<th>Upload Date</th>'; html += '<th>Download</th>'; html += '<th></th>'; html += '</thead>'; html += '<tbody>'; $.each(Result.d, function (key, value) { html += '<tr>'; html += '<td>' + value.UserName + '</td>'; html += '<td>' + value.DateStr + '</td>'; html += '<td>' + '<a href="' + value.FilePath + '">Download</a>' + '</td>'; html += '<td>' + '<input type="button" value="Delete" onclick="deleteUploadFile(this,' + value.Id + ')" >' + '</td>'; html += '</tr>'; }); html += '</tbody>'; html += '</table>'; $("#uploaddata").html(html); }, error: function (e, x) { alert(x.ResponseText); } }); }
解决方案In function signature you should have the parameter name. While calling the function you should pass the guid to the function. See the example below:
MARKUP:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <script> // Notice "id" is the parameter name here function LoadUserUploadFiles(id){ alert(id); }; </script> <input id="test" type="button" value="test" onclick='LoadUserUploadFiles("<%=GetUserID()%>")' /> </div> </form> </body> </html>
CODE:
using System;
public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } public Guid GetUserID() { Guid currentUserId = (Guid)Membership.GetUser().ProviderUserKey; return currentUserId; } }
EDIT: You can change you code like this:
<script> $(function () { LoadUserUploadFiles('<%=GetUserID()%>'); function LoadUserUploadFiles(id) { alert(id); var url = '<%= ResolveUrl("/WebMethods.aspx/GetIndividuallyUploadedFiles") %>'; $.ajax({ //Rest of the code goes here
这篇关于通过服务器端UserGUID到客户方Ajax调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文