How does data: {} or whatever work in Ajax? - ajax

function Autobuy(id, price){
$.ajax({
type: "GET",
url: "http://m.roblox.com/Catalog/VerifyPurchase?assetid=" + id + "&type=robux&expectedPrice=" + price,
success: function(Data){
var link = "http://m.roblox.com/Catalog/VerifyPurchase?assetid=" + id + "&type=robux&expectedPrice=" + price
var Regex = /__RequestVerificationToken" type="hidden" value="(.+)" \/>/
var Verify = Data.match(Regex)[1]
$.ajax({
type: "POST",
url: "http://m.roblox.com/Catalog/ProcessPurchase",
data: //idk what to do here to prevent internal 500 serv error
__RequestVerificationToken: Verify,
CurrencyType: 1,
AssetID: id,
ExpectedPrice: price
});
}
});
};
How do you use data: with $.ajax({ })?
If I don't use data: then it gives me an error in the function I made.

It's the result from your GET request, in fact it contains whatever the server returns from it(integer, string, array...) if there are no errors(that's why it's passed as parameter in success:).

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){
}
});
}

Send FormData and String Data Together Through JQuery AJAX

I have the below that uploads an image using JQuery's AJAX function
var id = <?php echo $id; ?>;
var img_data = new FormData($("form")[0]);
$.ajax({
url: 'add.php',
data: img_data,
contentType: false,
processData: false,
type: 'POST',
success: function(data){
alert(data);
}
});
I'd like to include a string with the FormData sent. I tried the following, but no luck
data: img_data {id: id},
What's the correct syntax here?
Use append
var img_data = new FormData($("form")[0]);
img_data.append('id', id);
You could create a JSON data object and pass that as application/json and process the data within add.php:
var data = {
id : <?php echo !empty($id) ? $id : "''",
img_data : new FormData($("form")[0])
};
$.ajax({
url: 'add.php',
data: data,
contentType: "application/json",
type: 'POST',
success: function(data){
alert(data);
}
});
Although unconventional, you could also append the data as a query string to the URL with the POST. You'd also need to edit add.php to get this parameter.
$.ajax({
url: 'add.php?id=' + id,
data: img_data,
processData: false,
type: 'POST',
success: function(data){
alert(data);
}
});

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));

jquery ajax return with html result

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 ?

ajax post getting value in CLASSIC asp of html

I am trying to get the value i am sending from one page to another using jquery ajax json.
This is my code:
function checkTheVin()
{
$.ajax({
type: "POST",
url: "checkVin.asp",
data: 'theVIN=' + $("#vwVin").val(),
cache: false,
dataType: "html",
beforeSend: function() {$.blockUI({ message: '<img src="img/ajax-loader.gif" />' });},
complete: function(){$.unblockUI();},
success: function(responseText){
if (responseText.indexOf("GOOD") > -1)
{
$("#theContent").html(responseText.replace("GOOD",""));
}else{
//alert(data);
}
},
error: function(responseText){alert('err: ' + responseText);},
});
}
However i never get a resonse back. It's null.
This is how i am getting it using CLASSIC asp:
dim vwVin
vwVin = request.QueryString("theVIN")
What am i doing incorrect?
David
Try using a GET method in your ajax call:
$.ajax({
type: "GET",
url: "checkVin.asp",
data: 'theVIN=' + $("#vwVin").val(),
cache: false,
dataType: "html",
beforeSend: function() {$.blockUI({ message: '<img src="img/ajax-loader.gif" />' });},
complete: function(){$.unblockUI();},
success: function(responseText){
if (responseText.indexOf("GOOD") > -1)
{
$("#theContent").html(responseText.replace("GOOD",""));
}else{
//alert(data);
}
},
error: function(responseText){alert('err: ' + responseText);},
});
You have three options
1. Use the Request Object with out specifying the collection
dim vwVin
vwVin = request("theVIN")
The web server will then search request collections for you, first the querystring and then the form.
2. Specify the Request.Form collection if you are using Ajax Post
$.ajax({ type: "POST", ...
dim vwVin
vwVin = Request.Form("theVIN")
3. Specify the Request.QueryString collection if you are using Ajax GET
$.ajax({ type: "GET", ...
dim vwVin
vwVin = Request.QueryString ("theVIN")

Resources