I have an array JSON encode respond when the ajax JSON throw a post request (refer below).
requestparser.php:
$array = array("phweb" => "yes", "phemail" => "yeeess");
echo json_encode($array);
And this Ajax JSON use for sending post request to requestparser.php and processing the return response.
$.ajax({
type: 'POST',
url: 'requestparser.php',
data: { "request" : "pull" },
contentType: "application/json; charset=utf-8",
dataType: 'json',
cache: false,
success: function(result) {
alert(result[0]);
alert(result[1]);
}
});
I want to get the value of array key phweb and the value of array key phemail yet when an alert box popup, it says undefined. What seems the problem? Any help, ideas, clues would be greatly appreciated.
So far what I tried is:
$.ajax({
type: 'POST',
url: 'requestparser.php',
data: { "request" : "pull" },
contentType: "application/json; charset=utf-8",
dataType: 'json',
cache: false,
success: function(result) {
alert(result[0]->phweb);
alert(result[1]->phemail);
}
});
And sadly, it doesn't work.
The result is a JSON object. You can access it like this
success: function(result) {
alert(result['phweb']);
alert(result['phemail']);
}
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 tried to find information in google, but no results there. I want send to server array with keys ('data':'unknown','datakey':'status') and file.
I tried this one, but settings contentType:false and processData:false removing keys:
$('#null').on('click', function(efile) {
var inFile = new FormData();
inFile.append('outFile', efile.target.files[0]);
});
$.ajax({
cache:false,
contentType:false,
processData:false,
url:'fileservice.php',
data:{'data':'unknown','datakey':'status', inFile},
type:"POST",
success: function(eresponse) {
alert(eresponse);
}
});
"contentType" is type of data you sending such as 'application/json; charset=utf-8'
Default is: "application/x-www-form-urlencoded"
Try below code
$.ajax({
url:'fileservice.php',
cache:false,
contentType: "application/json; charset=utf-8",
data : JSON.stringify({'data':'unknown','datakey':'status', inFile}),
processData:false,
type:"POST",
success: function(eresponse) {
alert(eresponse);
}
});
Let me know if still not solved you problem.
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 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.