HTTP POST via JQuery
i am working on API, The API supports both HTTPS POST and HTTPS GET methods.
Moreover, it is a security risk to expose Web Registration credentials on a public web site. Instead, use a background HTTP POST (server side) to initiate the web registration.
how can i http post (aspx) via jquer开发者_运维百科y?
If you want to post via AJAX, you can use jQuery's $.post
method.
$('form').post('.path/to/url', $('form').serialize(), function(data){
alert('posted);
});
Or, you could always just use an old-fashioned form:
<form action="/path/to/url" method="POST">
there is a jQuery.post method that enables you to post any data you want
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);
}
});
and then the server part:
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\"}");
Read here http://api.jquery.com/jQuery.post/ or here http://api.jquery.com/jQuery.ajax/
精彩评论