I need to post to this URL
https://liceoeuroamericano.territorio.la/webservices/persona.php
I need to post 2 different Parameters
method
param
method=activateUser
param
{
"auth_key": "123456",
"user": "[email]",
"active":"[0/1]"
}
string(JSON)
What I have Tried - AM I doing anything right?
<button id="activateUser">GO</button>
<script>
$('#activateUser').on('click', function(){
$.post('https://liceoeuroamericano.territorio.la/webservices/persona.php',
{
method=activateUser¶ms={
"auth_key": "123456",
"user": "[email]",
"active":"[0/1]"
}
string(JSON)
}, function(data){
})
});
</script>
If I've understood your requirements you need to pass method as a string, then JSON encode the params object. If so, this should work for you:
$.post('https://liceoeuroamericano.territorio.la/webservices/persona.php', {
method: 'activateUser',
params: JSON.stringify({
auth_key: "123456",
user: "[email]",
active: "[0/1]"
})
}, function(data){
console.log('request completed successfully');
})
Related
I want to Post a Json string or PHP array to a Restful API and get the response without reloading the page. So, I want to use Ajax and/or jquery. So far, I'm failing miserably. my code is below.
I've tried several jquery methods - some using stringify and others just sending raw json. I'm not getting enough response or error of any kind to know where I need to look to solve the problem.
$(".avm_zip").on("click", function() {
$.ajax({
url: "REMOVED",
type: "POST",
data: {
"Header": {
"UserId": "REMOVED",
"Password": "REMOVED",
"SourceApplication": "Dev Service Tester"
},
"Property": {
"StreetAddress": "10345 W Briar Oaks Dr # E",
"City": "Stanton",
"State": "CA",
"Zip": "90680",
"APN": null,
"PropertyType": null
},
"Order": {
"CustomerOrderReference": "Test LITE",
"PreferenceTableName": "VeroLITE TEST",
"Price": null,
"Credit": null,
"AdditionalFields": null
}
},
dataType:'json',
success: function (response) {
alert("Success");
},
error: function(error){
alert("Something went wrong");
},
});
The API is more robust and centered on XML. Getting the basic call & retrieval set up using Json had challenges because of this. So, it is possible that is bleeding over into this effort.
I have a problem to use ajax on Laravel to each data on relationship one to many. I'm trying to use ajax but return undefined.
This is Code if using Laravel foreach
<div id="project-data">
#foreach ($projectstatus as $project)
<li>{{$project->status}}</li>
#foreach($projectstatus->project as $projectlist)
<li>{{$projectlist->project_name}}</li>
#endforeach
#endforeach
</div>
I'm trying using jquery for each This is My Code
function getDataProyek(){
$.ajax({
url: "{{ url('dt.proyek') }}",
type: "GET",
dataType: "JSON",
success : function(data) {
jQuery.each(data, function(index,project) {
$('#proyek-data').append('<li>'+project.status+'</li>'+
<li>'+project.project.project_name+'</li>');
});
}
});
}
This is My Controller
public function dtproyek()
{
$projectstatus= Status::with('project')->orderBy('created_at','ASC')->get();
return $projectstatus;
}
This is json return
[
{
id: "1",
status: "Waiting",
created_at: "2018-07-30 12:24:25",
updated_at: "2018-07-30 12:24:25",
project: [
{
id: "2",
project_name: "Project 1",
created_at: "2018-08-11 11:59:51",
updated_at: "2018-09-24 07:49:38",
}
]
}
]
In JSON result project is an array and you must use index for that on getDataProyek function or use a loop for that
$('#proyek-data').append('<li>'+project.status+'</li>'+
'<li>'+project.project[0].project_name+'</li>');
and in your controller return json data
return response()->json($projectstatus);
Update:
$.each(data, function(index,project) {
var str='<li>'+project.status+'</li>';
$.each(project.project, function(key,val) {
str+='<li>'+val.project_name+'</li>';
});
$('#proyek-data').append(str);
});
I have a json file that has about twenty objects like this:
{
"firstName": "User1",
"lastName" : "UserLname",
"title": "Human",
"description": "Something Facinating",
"photoURL": "http://www.example.com/wp-content/themes/mytheme/images/profile/user1.jpg",
"contact": {
"email" : "user1#example.com"
}
}
I have a javascript code to display images/description from these objects to a page. I want this site to be uploaded in more than one place. So it doesn't make sense for me to use absolute url in this json file. I overcame the issue in js by passing a variable templateUrl from header.php file and calling it inside the javascript file.
<script type="text/javascript">
var templateUrl = '<?= get_bloginfo("template_url"); ?>';
</script>
And in javascript:$.getJSON(templateUrl+"/scripts/file.json", function(file){....}
I want a way to pass this templateUrl variable to json file too. So I can have image path set to just images/profile/user1.jpg, and I can prepend the url to this depending on where the site is uploaded.
#json.php
{
"firstName": "User1",
"lastName" : "UserLname",
"title": "Human",
"description": "Something Facinating",
"photoURL": "<?= $_GET['var']; ?>/images/profile/user1.jpg",
"contact": {
"email" : "user1#example.com"
}
}
#main page
<script type="text/javascript">
var templateUrl = encodeURI("http://example.com");
$(function(){
$.getJSON(
'json.php?var=' + templateUrl,
function(data){
$.each(data, function(key, val){
console.log(key + ": " + val);
});
}
);
});
</script>
Output:
firstName: User1
lastName: UserLname
title: Human
description: Something Facinating
photoURL: http://example.com/images/profile/user1.jpg
contact: [object Object]
I've a php script that has the following line:
echo json_encode(array('success'=>'true','userid'=>$userid, 'data' => $array));
It returns the following:
{
"success": "true",
"userid": "1",
"data": [
{
"id": "1",
"name": "Trigger",
"image": "",
"subtitle": "",
"description": "",
"range1": null,
"range2": null,
"range3": null
},
{
"id": "2",
"name": "DWS",
"image": "",
"subtitle": "",
"description": "",
"range1": null,
"range2": null,
"range3": null
}
]
}
But when I call a jQuery ajax as below:
$.ajax({
type: 'POST',
url: 'url',
crossDomain: true,
data: {name: name},
success: function(success, userid, data) {
if (success = true) {
document.write(userid);
document.write(success);
}
}
});
The userid is 'success'. The actual success one works, its true.
Is this malformed data being returned? Or is it simply my code?
Thanks in advance,
Niall
The arguments the success callback takes are defined in the documentation:
success(data, textStatus, jqXHR)
The response is not split up before being passed as arguments to it. You have to extract the values from the first argument.
You also need to add header('Content-Type: application/json') to your PHP so that jQuery will parse the response as JSON instead of as HTML when populating the first argument.
You can then test data.success and access data.userid (as well as data.data which will be the array assigned to the data property in the returned data… you might want to rename it to avoid confusion).
Note that when you test it you need to use === (or ==), not the * assignment* operator you are using at present. Also note that your JSON response is returning the string "true" and not the boolean true.
You can't add your own arguments in Success. Change your code like this,
success: function(response) {
if (response.success == true) {
document.write(response.userid);
document.write(response.success);
}
}
According to jQuery Docs for success(),
success(data, textStatus, jqXHR)
A function to be called if the request succeeds. The function gets passed three arguments: The data returned from the server, formatted according to the dataType parameter; a string describing the status; and the jqXHR (in jQuery 1.4.x, XMLHttpRequest) object. As of jQuery 1.5, the success setting can accept an array of functions. Each function will be called in turn.
http://api.jquery.com/jQuery.ajax/
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Loop through Json object
I have a PHP function, data.php, which fetches JSON data from an external server URL like such:
<?php
$url = "https://dev.externalserver.net/directory";
$content = file_get_contents($url);
echo json_encode($content);
?>
The JSON array retrieved looks like the following:
[
{ "name": "Not configured",
"mac_address": "1111c11c1111",
"online": "false",
"rate": "Not configured" },
{ "name": "Not configured",
"mac_address": "0000c00c0000",
"online": "false",
"rate": "Not configured" }
]
I'm now trying to write an AJAX call to that PHP function, iterate through the JSON array, and display it in the browser in non-JSON formatting. My AJAX code looks like the following:
$.ajax({ url: 'data.php',
type: 'POST',
dataType: 'json',
success: function(output) {
$.each(output, function() {
$.each(this, function(key, value){
alert(key + " --> " + value);
});
});
}
});
My issue is that code is currently displaying alert boxes that show the individual characters within the array like such: 0 --> [ , 0 --> , 0 --> { ... etc etc
Is there a problem with how I'm passing the data using json_encode(); and dataType: 'json' or does the issue resolve with how I'm iterating through the array?
Thanks.
Other responders missed the sneaky but still obvious bit, that what's coming back from the resource being polled in the PHP is probably already valid JSON, and re-encoding it is causing the browser to interpret it merely as a string. In this case, the javascript never had a chance.
Remove the json_encode() bit in the PHP and just echo what comes back, and see if that doesn't improve things.
I think your problem is the this in the second each. Try:
$.each(output, function(key, value) {
$.each(value, function(key, value){
alert(key + " --> " + value);
});
});
var jsonArray = [
{ "name": "Not configured",
"mac_address": "1111c11c1111",
"online": "false",
"rate": "Not configured" },
{ "name": "Not configured",
"mac_address": "0000c00c0000",
"online": "false",
"rate": "Not configured" }
];
$.each(jsonArray, function() {
for (var key in this) {
if (this.hasOwnProperty(key)) {
console.log(key + " -> " + this[key]);
}
}
});
take a look at this
How do I loop through or enumerate a JavaScript object?