I try to understand one thing.
I want to post an object with jquery Ajax POST , something like this:
var dataPostYear = {
viewType:GetViewType(),
viewDate:'2009/09/08',
languageId:GetLanguageId()
};
$.ajax({
type: "POST",
url: url,
data: dataPostYear,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnLoadYearListSuccess,
error: OnLoadYearListError
});
and it doesn't work.
But this one works fine:
var dataPostYear = "{viewType:'"+ GetViewType() + "',viewDate:'2009/09/08',languageId:'"+GetLanguageId()+"}";
$.ajax({
type: "POST",
url: url,
data: dataPostYear,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnLoadYearListSuccess,
error: OnLoadYearListError
});
GetViewType() return --'0'
languageId() return --'1'
it's just a string
there is a way to post an object, something what I try to do in my first way ? Or not ?
Thanks
Use jQuery.param(). Here is the documentation
You should look at .postJSON.
Essentially, you just add json as a 4th argument to the $.post
From the site:
// Send the request
$.post('script.php', data, function(response) {
// Do something with the request
}, 'json');
If you want the .ajax call version, you can convert it using the .post docs.
Related
The task is very simple I only need to save these json responses into my code to manipulate it later but I can't get to make it function. I've tried more than a strategy with nothing working. Thing is it's working for a json and not the other with the exact same syntax. Here is my code
function searchOwner() {
$.ajax({
type: "GET",
contentType: "application/json",
dataType: "jsonp",
url: "https://elvet.eu-gb.mybluemix.net/get_pet_owner?pet_id=p12345678912345",
success: function(responseData) {
alert(responseData)
}
})
}
function searchPet() {
$.ajax({
type: "GET",
contentType: "application/json",
dataType: 'jsonp',
url: "https://elvet.eu-gb.mybluemix.net/getpets?pet_id=p12345678912345",
success: function(responseData) {
alert('ok')
}
});
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
The SearchOwner is not working at all while searchPet is working just fine. Any help would be highly appreciated thank you
dataType : "json", in searchOwner() where as it's dataType: 'jsonp' in searchPet()
I am trying to pass a single quoted string in data without key.
I write a code below :
$.ajax({
type: "POST",
url: 'MY_API_URL',
data: "HATETH",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (r) {
console.log(r);
}
});
it not works..
Please help me to do this. Is it possible or not?
I don't know what's wrong. I've spent almost an hour reading and re-reading, checking my spelling etc. I was hoping maybe someone can point out what I'm doing wrong.
This is the cURL statement that returns successfully in the terminal:
curl https://api.gumroad.com/v2/products \
-d "access_token=123456abcdef" \
-X GET
The following are some of my attempts that did not work. And yes, I'm certain jQuery has been loaded:
$.ajax({
url: "https://api.gumroad.com/v2/products",
data: "access_token=123456abcdef",
success: function(result){
console.log(result);
}});
And this one:
$.ajax({
url: "https://api.gumroad.com/v2/products",
data: "access_token=123456abcdef",
processData: false,
type: "get",
success: function(result){
console.log(result);
}});
And another one:
$.ajax({
url: url,
beforeSend: function(xhr) {
xhr.setRequestHeader("access_token", "123456abcdef")
}, success: function(data){
alert(data);
//process the JSON data etc
}
})
Looks like you are sending a string instead of a data object, try this:
$.ajax({
url: "https://api.gumroad.com/v2/products",
data: {access_token: "123456abcdef"},
processData: false,
type: "get",
success: function(result) {
console.log(result);
}
});
I have a problem. I'm trying to send content of a textarea with an ajax call, but it doesn't seem to be working, and I don't know why.
There's the method called GetStatus(string statusText) which need to receive the content.
Here's the javascript code:
$("#btnSaveStatus").on("click", function () {
var statusText = $(".textareaEdit").val();
$.ajax({
type: "GET",
url: "Default.aspx/GetStatus",
data: "{statusText:'" + statusText + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (result) {
// $('#littlbioID').text(result.d);
}
});
});
Please advise. You should also know that I'm new into web development.
You can't have a request body in a GET request, you have to use a POST request for that
The string you are constrcting is not valid JSON since:
Property names must be strings
You have no idea what the user will enter in the textarea - it might contain characters with special meaning in JSON
Generate your JSON programatically.
{
type: "POST",
url: "Default.aspx/GetStatus",
data: JSON.stringify({
statusText: statusText
}),
// etc
Obviously, the server side of the process needs to be set up to accept a POST request with a JSON body (instead of the more standard URL Form Encoded format) as well.
Try this:
$("#btnSaveStatus").on("click", function () {
var statusText = $(".textareaEdit").val();
var jsonText = new Object();
jsonText.statusText = statusText;
$.ajax({
type: "POST",
url: "Default.aspx/GetStatus",
data: JSON.stringify(jsonText);,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (result) {
// $('#littlbioID').text(result.d);
}
});
});
I have this url which brings back the yahoo time...im guessing the PST
so i need to get this value with javascript...here is my code
$j.ajax({
type: "GET",
url: "http://developer.yahooapis.com/TimeService/V1/getTime?appid=YahooDemo&output=json",
dataType: "jsonp",
complete: function(data){
console.log(data);
}
});
but i cant seem to pull out that Timestamp out of the json...what am I doing wrong
You're using the complete method, which returns the XHR object, not the result.
You want success:
$j.ajax({
type: "GET",
url: "http://developer.yahooapis.com/TimeService/V1/getTime?appid=YahooDemo&output=json",
dataType: "jsonp",
success: function(data){
console.log(data.Response.Timestamp);
}
});
Source: http://api.jquery.com/jQuery.ajax/
I think you want to use the success callback:
$j.ajax({
type: "GET",
url: "http://developer.yahooapis.com/TimeService/V1/getTime?appid=YahooDemo&output=json",
dataType: "jsonp",
success: function(data,status,xhr){
console.log(data.Result.Timestamp);
}
});
The JSON looks like {"Result":{"Timestamp":1331089290}}. That is, an object property called Result, which is another object literal containing the property Timestamp:
// Use .success rather than .complete
success: function(data){
console.log(data.Result.Timestamp);
}
javascript:
//change
dataType: "jsonp",
//to
dataType: "json",
Then extract the timestamp with data.Result.Timestamp.
When you use the value, remember that UNIX timestamp is in seconds whilst the javascript Date object works in milliseconds.