POST Request in Ruby [duplicate] - javascript

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Make this HTTP POST request in ruby
How do you perform the following JavaScript request in Ruby?
url = 'http://www.example.com/';
data 'name=aren&age=22';
$.ajax({
type: "POST",
url: url,
data: data,
dataType: "json",
error: function() {
console.log('error');
},
success: function(data) {
console.log('success');
}
});

Check out HTTParty, great gem https://github.com/jnunemaker/httparty
Example:
response = HTTParty.post(url, body: data)
puts response.body, response.code, response.message, response.headers.inspect
Although I believe it is synchronous- dont quote me on that though

Related

Posting JSON with variable using Ajax in [duplicate]

This question already has answers here:
jQuery posting valid json in request body
(2 answers)
Closed 4 years ago.
I was trying to put variable into JSON. I want to post it using Ajax.
My code:
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
var user_Details = "1528205024";
function checkUserForDashboard(){
$.ajax({
url: "api comes here",
type: "POST",
data: {"user_id": user_details },
dataType: "json",
crossDomain : true,
success: function (data) {
console.log(data);
}
})};
</script>
The post request gives: bad request error.
Enclose your JSON object into JSON.stringify() to ensure your json object is serialized in a safe string.
Also, set the content-type property.
$.ajax({
url: "api comes here",
type: "POST",
contentType: "application/json; charset=utf-8",
data: JSON.stringify({"user_id": user_details }),
dataType: "json",
crossDomain : true,
success: function (data) {
console.log(data);
}
})};

AJAX how to pass api key in header [duplicate]

This question already has answers here:
CORS error when jquery ajax request on api
(2 answers)
Closed 5 years ago.
I have here a strange situation with AJAX call, how to pass api key in header:
My full url for my json is: https://apifootball.com/api/?action=get_events&from=2017-10-30&to=2017-11-01&APIkey=fd6b8ec7d651960788351ee2b1baffba6ac1a9c8eb047118a1a823c247bdade0
And I am trying now to pass API key in headers of ajax call, but still have this error from console:
"Failed to load https://apifootball.com/api/?action=get_events&from=2017-10-30&to=2017-11-01&: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://bigsportlive.com' is therefore not allowed access."
Here is my ajax call:
var apiKey = "fd6b8ec7d651960788351ee2b1baffba6ac1a9c8eb047118a1a823c247bdade0";
$.ajax({
type: "GET",
url: "https://apifootball.com/api/?action=get_events&from=2017-10-30&to=2017-11-01",
headers: { "APIkey": apiKey },
success: function(result){
result[i].league_name
}
});
May be I am doing something not correct?
Thanks!
If you want to add a header (or a set of headers) to each request, use the beforeSend hook with $.ajaxSetup ():
$.ajaxSetup({
beforeSend: function(xhr) {
xhr.setRequestHeader('x-my-custom-header', 'some value');
}
});
// Sends your custom header
$.ajax({ url: 'your/url' });
// Sends both custom headers
$.ajax({ url: 'your/url', headers: { 'x-some-other-header': 'some value' } });
Another solution consist to use lowercase for headers
$(document).ready(function () {
$.ajax({
url: "http://xx.xx.xx.xx:xx/api/values",
type: "GET",
dataType: "json",
headers: { "HeaderName": "MYKey" }
});
});
Yes, there is an Access-Control-Allow-Origin error.
If so, you may want a php backend to get this for you, I believe, using
<?php
$data = file_get_contents("https://apifootball.com/api/?action=get_events&from=2017-10-30&to=2017-11-01&APIkey=fd6b8ec7d651960788351ee2b1baffba6ac1a9c8eb047118a1a823c247bdade0");
echo json_encode($data);
?>
Then use an ajax call to this file.
$.ajax({
type: "GET",
url: 'name_of_php_file.php',
dataType: "json",
success: function(result){
alert(result);
}
});

Serialize form and combine with other value in ajax data [duplicate]

This question already has answers here:
jQuery: serialize() form and other parameters
(12 answers)
Closed 5 years ago.
I have Ajax like this.
var idlapkondisi = $('#id_laporan_pemeriksa').val();
var data = $('#myFormkondisi').serialize();
$.ajax({
type:'ajax',
method:'POST',
url:url,
dataType:'json',
success:function(response){
},
error:function(response){
console.log(response);
}
})
})
how can i make 2 value in ajax data while the value is a serialize data form ?
does the format like this ?
data:{data,idlapkondisi:idlapkondisi},
Use serializeArray instead of serializing.
var idlapkondisi = $('#id_laporan_pemeriksa').val();
var data = $('#myFormkondisi').serializeArray();
data.push({name: "idlapkondisi", value: idlapkondisi});
$.ajax({
type: 'ajax',
method: 'POST',
url: url,
data: data,
dataType: 'json',
success: function(response) {
},
error: function(response) {
console.log(response);
}
});
Try this to add that value to you payload:
var idlapkondisi = $('#id_laporan_pemeriksa').val();
var data = $('#myFormkondisi').serialize();
data["idlapkondisi"] = idlapkondisi;
Then you can add data to your Ajax call as usual.

Callback in ajax request [duplicate]

This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 6 years ago.
I have made API and trying use it in my jQuery scripts. But having problems with callbacks.
My JavaScript:
function findAll() {
var users;
$.ajax({
url: "/user",
type: "GET",
dataType: "json",
success: function (data) {
users = data;
}
});
return users;
}
Help me return received data, please.
Thanks in advance.
This will not work because you are returning users synchronously. The value will be undefined.
What is happening is this:
- var users is declared and set to undefined by the Javascript engine
the AJAX callback is registered to be called when the XHR request resolves
var users (now = undefined) is returned
users is assigned the response of the XHR request. But alas! It's too late and undefined has already been returned!!
This should work:
function returnUsers(users) {
return users;
}
var users = findAll(returnUsers);
function findAll(callback) {
$.ajax({
url: "/user",
type: "GET",
dataType: "json",
success: callback
});
}

Making AJAX Call For Use In Multiple Methods [duplicate]

This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 7 years ago.
I have to make numerous AJAX calls and want to set up a simple method to handle this. I created the following
function testingAjax(url) {
$.ajax({
type: "GET",
contentType: "application/json; charset=utf-8",
url: url,
data: "{}",
dataType: "json",
success: function(data) {
//alert(data);
return data;
},
error: function(err) {
alert('error')
}
});
};
When I call testingAjax from another method, I get an Undefined error, yet when I alert from within testingAjax it is correct.
function testingCall(){
var data = testingAjax(url);
alert(data);
}
Basically, I want to return the data from the AJAX call, and parse it elsewhere. What am I doing wrong?
You'll want to add a callback param (which is a function) and pass it to success in place of what you already have.
function testingAjax(url, cb) {
$.ajax({
type: "GET",
contentType: "application/json; charset=utf-8",
url: url,
data: "{}",
dataType: "json",
success: cb,
error: function(err) {
alert('error')
}
});
};
To use it:
testingAjax('myurl', function(data) {
console.log(data);
});
This will take the value of the success callback and immediately pass it as an argument to your custom callback, which will allow you access to data.

Categories

Resources