jquery ajax return with html result - ajax

I am using jquery ajax to post username and password and return with a result it with working perfectly with GET method but using the post method it send the data but not return with the html result
here is my code:
$.ajax({
type: "POST",
url: "panel.aspx",
data: username + ";" + pw,
success: function (result) {
$("#midiv").html(result);
}
});

Try;
$.ajax({
type: 'POST',
url: 'panel.aspx',
data: {
'username=' + uname "&password=" + pword,
//Note:- uname and pword are variables and not text
},
success: function (result) {
$("#midiv").html(result);
}
});
in your aspx, you may catch data something like;
Dim uname, pword
uname = Request.Form("username")
pword = Request.Form("password")
Hope this helps...

Try this:
$.ajax({
type: "POST",
url: "panel.aspx",
data: {
username: "foo",
pw: "bar"
},
success: function (result) {
$("#midiv").html(result);
}
});
The way you are doing you are sending the variables in the GET request URL. To send data via post, define an object in the data config which the keys represent the parameters that you want to send.

You are not sending the data correctly, try:
$.ajax({
type: "POST",
url: "panel.aspx",
data: 'username=' + username + "&password=" + pw,
success: function (result) {
$("#midiv").html(result);
}
});

$.ajax({
type: "POST",
url: "panel.aspx",
data: {username: username, password: pw}
}).done(function(result) {
$("#midiv").html(result);
});
You'll also need to change your serverside scripts to listen for POST requests and not GET requests, for instance in PHP it would be:
$user = $_POST['username'];
$pw = $_POST['password'];
Not really sure how to do it in aspx, but I'm guessing you'll figure that out ?

Related

Pass Parameter in Ajax URL

I am trying to pass parameter in below ajax url
function endprogress(){
$("#surfbar").html("Please wait...");
var dataString = 'action=validate&t='+adtk;
$.ajax({
type: "POST",
url: 'yes_manage.php?view=surf&track='+user_name&, /*this line giving error*/
data: dataString,
success: function(msg){
}
});
}
above url field is giving error Expected and identifier instead saw ','
how can I resolve this
Change user_name& => user_name
look like
function endprogress(){
$("#surfbar").html("Please wait...");
var dataString = 'action=validate&t='+adtk;
$.ajax({
type: "POST",
url: 'yes_manage.php?view=surf&track='+user_name, /*remove & at the end user_name*/
data: dataString,
success: function(msg){
}
});
}

Receive data at node.js server from Ajax call in Object format

I am using Ajax call to post data from client side to node server and trying to receive data at server end, manipulate it(do some db query) and then return the response.
client side code :
$.ajax({
type: "post",
url: "http://localhost:8888/ajaxRequest",
dataType: "json",
data: {name: "Manish", address: {city: "BBSR", country: "IN"}}
}).done(function ( data ) {
console.log("ajax callback response:" + data);
});
server side :
var port = 8888;
var server = http.createServer();
server.on('request', request);
server.listen(port);
function request(request, response) {
var store = '';
response.writeHead(200, {"Content-Type": "text/json"});
request.on('data', function(data) {
store += data;
});
request.on('end', function() {
console.log(store);
response.end(store);
});
}
Problem : when I am consoling "store" variable in request end function I am getting something like this:
name=Manish&address%5Bcity%5D=BBSR&address%5Bcountry%5D=IN
I just want the same data at the server side what I sent in the Ajax 'data' parameter. I also tried, queryString.parse, JSON.parse() but no help. I just want my output as :
{name: "Manish", address: {city: "BBSR", country: "IN"}}
You need to tell jQuery that this is an JSON request:
$.ajax({
type: "post",
url: "http://localhost:8888/ajaxRequest",
dataType: "json",
contentType: "application/json; charset=UTF-8",
data: JSON.stringify({name: "Manish", address: {city: "BBSR", country: "IN"}})
}).done(function ( data ) {
console.log("ajax callback response:" + data);
});
This way, your request body will reach the server with the stringified JSON, so you'll be able to do the following:
request.on('end', function() {
store = JSON.parse(store);
console.log(store); // ta-daaa, Object!
response.end(store);
});
Have you tried something like:
...
response.end(JSON.stringify(store));

How to get content from my router with AJAX

Let me explain in details what I want... I need to get content from my router via this address http://user:password#192.168.1.1/dhcp_table.html from this line (class name):
<td class="data_table_data" align="center">
**PC name**
</td>
I would like to see my result like this:
PC name
Is this what I am looking for? If yes, then how?
$.ajax({
url: url,
data: data,
success: success,
dataType: dataType
});
You can pass username and password to the router as ajax parameters. You should also specify dataType: 'html' as well:
var url = "http://192.168.1.1/dhcp_table.html",
username = "<username here>",
password = "<password here>";
var auth = 'Basic ' + Base64.encode(username + ':' + password);
$.ajax
({
type: "GET",
url: url,
dataType: 'html',
username: username,
password: password,
headers : { Authorization : auth },
success: function (data) {
var dhcpTable = $(data);
alert('Text of table = '+dhcpTable.filter('.data_table_data').text());
}
});
Edit: added base64 authentication.

jQuery Ajax not executing

Greeting.
I have made this jQuery Ajax call, which doesn't seems to be executing.
It does handle the .live() call, but not the .ajax() call.
Any ideas?
Source of ajax.js:
$("#action_login").live("click", function() {
var username = $("#login_form input.username").val();
var password = $("#login_form input.password").val();
alert("Username: " + username + "/nPassword: " + password);
$("#login_form .status").ajax({
type: 'POST',
url: 'ajax/login.php',
data: {
'username': username,
'password': password
},
success: function(data) {
alert(data);
},
error: function(data) {
alert("error" + data);
},
dataType: dataType
});
});
It does call the first alert(); command, with the right value from the inputs for username and password, but after that nothing happens.
Hope someone can help me out.
$("#login_form .status") is the call to the $ function with "#login_form .status" as parameter.
It returns a very different object than $, and it doesn't have any ajax function.
So, replace
$("#login_form .status").ajax(
with
$.ajax(
Replace
$("#login_form .status").ajax
With
$.ajax

ajax POST method is not working

I am trying to send the data via ajax POST method my code is
$.ajax({
url: myUrl + "?token=" + accessToken + "&key=" +dev_key,
dataType: 'jsonp',
type: 'POST',
data: sendXML,
success: function () {
alert("z");
}
});
But the type: 'POST' is not working I am getting the following error on console:
Status Code:405 HTTP method GET is not supported by this URL
Have you tried using $.post ?
Example:
$.post(
myUrl,
{
token: accessToken,
key: dev_key
},
function(result){
alert(z)
}
)
P.S. Isn't ? missing after myUrl?
i think you forgot the ? in the token key like this
mySql + "?token="
otherwise, try this:
jQuery.post(
myUrl + "?token=" + accessToken + "&key=" +dev_key,
sendXML,
function() {
alert('z');
},
'JSONP'
);

Resources