2011-03-02 43 views
0

我正在使用API​​,API支持HTTPS POST和HTTPS GET方法。通过JQuery的HTTP POST

此外,在公共网站上公开Web注册凭证是一种安全风险。相反,使用后台HTTP POST(服务器端)启动Web注册。

我怎样才能通过jQuery发布(aspx)?

+0

什么jquer y ajax与ASP.NET或Web注册或凭证有关吗?澄清你的问题。 – Aliostad 2011-03-02 15:33:54

回答

0

有一个jQuery.post方法,使您可以发布任何数据,你想

jQuery.post('Your.aspx', {userName:'someUsername', password:'somePass'},dataType:'json', function(data) 
{ 
    if(data.logged) 
     alert('welcome '+data.logged); 
    else 
    { 
     //invalid 
     if(data.error) 
      alert('error\r\n'+data.error); 

    } 
}); 

,然后服务器部分:

if (Request.HttpMethod == "POST") && !string.IsNullOrEmpty(Request["userName"] && !string.IsNullOrEmpty(Request["password"])) 
{ 
    try 
    { 
     bool logged = false; 
     //do what you need to do , set logged= true when ok... 
     if(logged) 
      Response.Write("{\"logged\":\"" + Request["userName"]+ "\"}"); 
     else 
      throw new Exception('Invalid credentials !'); 

    } 
    catch(Exception ex) 
    { 
     Response.Write("{\"error\":\"" + ex.Message+ "\"}"); 
    } 


} 
else 
    Response.Write("{\"error\":\"Invalid request\"}"); 
1

如果你想通过AJAX后,您可以使用jQuery的方法$.post

$('form').post('.path/to/url', $('form').serialize(), function(data){ 
    alert('posted); 
}); 

或者,你总是可以只用一个老式的形式:

<form action="/path/to/url" method="POST">