I am creating an array of objects in js and sending it to one of my controllers after stringify'ing the array.
var arr = new Array();
for(i=0;i<noOfDeals;i++){
var deals = {'percentageMin':document.getElementById("pmin"+i).value,
'percentageMax':document.getElementById("pmax"+i).value,
'modelApplicable': document.getElementById("model"+i).value,
'maxCashback' : document.getElementById("maxcash"+i).value,
'dealId' : document.getElementById("deal"+i).value
};
arr.push(deals);
}
alert(JSON.stringify(arr));
$.ajax({method:'get',url:'abc?data='+JSON.stringify(arr),success:function(response) {
//response = JSON.parse(response);
response = JSON.parse(response);
alert(response.body);
response = JSON.parse(response.body);
if(response.status != undefined && response.status == 'SUCCESS') {
alert('Merchant details updated successfully. Refresh the page to see the changes.');
}
else {
alert('Could not update merchant details, Some Error Occurred');
}
}});
In my controller i am encoding the data and then sending to hit the API :
public function updateselectedmerchants(){
if (isset($_GET['data'])) {
$str_data = $_GET['data'];
print_r(json_encode(array('deals' => $str_data)));
die;
}
}
The Output :
{"deals":"[{\"percentageMin\":\"1.00\",\"perentageMax\":\"0.00\",\"modelApplicable\":\"3\",\"maxCashback\":\"30.00\",\"dealId\":\"7\"}"}
The desired output :
{"deals":[{\"percentageMin\":\"1.00\",\"perentageMax\":\"0.00\",\"modelApplicable\":\"3\",\"maxCashback\":\"30.00\",\"dealId\":\"7\"}]}
There are three things which are unwanted in the output which is coming :
1) The double quotes before the first square brackets should not be there.
2) The ending square bracket is not present
3) "/" appearing
Please help me with this.
You should do
$str_data = json_decode($_GET['data'], true);
print_r(json_encode(array('deals' => $str_data)));
Otherwise $str_data remains a string, and will be JSON encoded as such, while it looks like you want it to be a PHP array structure, and then encode all of that into valid JSON again.
See this PHP 'fiddle'
You probably generate " or ' somewhere you dont want to.
Not sure if it work 100%, but try:
public function updateselectedmerchants(){
if (isset($_GET['data'])) {
$str_data = json_decode($_GET['data']);
print_r(json_encode(array('deals' => $str_data)));
die;
}
}
You should try PHP approach:
http://php.net/manual/en/function.urlencode.php
http://php.net/manual/en/function.urldecode.php
Just decode array and encode it on the other side.
Related
This question already has answers here:
Sending JSON to PHP using ajax
(8 answers)
Closed 5 years ago.
I have looked at a bunch of questions/answers on here and none of them seem to solve my problem. I am integrating a payment system that returns a JSON string via a JS library that I then need to parse on the server to compare the hash values to ensure that it is valid. So I get their response and attempt to pass it via jQuery AJAX to a PHP file on the server. The response I get from the payment system is valid JSON. If I pass it directly the result I get on the server seems to be URL encoded. If I JSON.stringify() it, it adds a bunch of extra quotes which results in invalid JSON.
function isValidJSON($str) {
json_decode($str);
return json_last_error() == JSON_ERROR_NONE;
}
$json_params = file_get_contents("php://input");
error_log($json_params);
//error_log($_POST['jsresp']);
//$respObj = json_decode(stripslashes($_POST['jsresp']));
//error_log($json_params);
if (strlen($json_params) > 0 && isValidJSON($json_params)) {
$respObj = json_decode($json_params);
} else {
error_log('bad json '.$json_params);
}
$result = 0;
$resp = json_encode($respObj->Response);
$hash = $respObj->Hash;
error_log($hash);
$calcHash = base64_encode(hash_hmac('sha512', $resp, $app->getSageClientSecret(), true));
error_log($calcHash);
if($hash === $calcHash) {
$result = 1;
}
$app->updateCartResponse($_COOKIE['qid'], $result);
And here is the jQuery AJAX call to send the data:
$(document).on('click', 'button#sps-close.sps.btn.btn-primary', function(){
var resp = $("#resp_holder").val();
$.ajax({
url: "<?=$env?>sources/processors/process_hash.php",
data: { jsresp : resp },
type: "post",
//dataType: "json",
success: function( data ) {
// nothing to do here.
}
});
var url = $("#redirect_url").val();
if(url != "") {
location.href = $("#redirect_url").val();
}
});
When I do it this way, the JSON that gets to the server looks like this:
jsresp%5Bresponse%5D=%7B%22requestId%22%3A%22443594%22%2C%22gatewayResponse%22%3A%7B%22status%22%3A%22Approved%22%2C%22reference%22%3A%22EBGHNHChw0%22%2C%22message%22%3A%22APPROVED+658658%22%2C%22code%22%3A%22658658%22%2C%22cvvResult%22%3A%22P%22%2C%22avsResult%22%3A%22+%22%2C%22riskCode%22%3A%2200%22%2C%22networkId%22%3A%2210%22%2C%22isPurchaseCard%22%3Afalse%2C%22orderNumber%22%3A%22443594%22%2C%22transactionId%22%3A%22NDViMWYzNmEwNWNiOGQxZjIwOTAwNzU4MmVjYzJhMWQ%22%2C%22timestamp%22%3A%222017-11-16T23%3A17%3A12.6584893-05%3A00%22%7D%7D&jsresp%5Bhash%5D=bgd1e0Cxhj5s1FQaUFFYk7BMnSIl4Ez1jPMopZFp%2B4MyN9chFZZoo%2F3IuZPX7bbQ%2BRyaReKN1CNJXxRmjnLMRQ%3D%3D
I don't understand what I'm doing wrong or how to get it there properly.
Based on your sample, this is what you get on the server
jsresp[response]={"requestId":"443594","gatewayResponse":{"status":"Approved","reference":"EBGHNHChw0","message":"APPROVED 658658","code":"658658","cvvResult":"P","avsResult":" ","riskCode":"00","networkId":"10","isPurchaseCard":false,"orderNumber":"443594","transactionId":"NDViMWYzNmEwNWNiOGQxZjIwOTAwNzU4MmVjYzJhMWQ","timestamp":"2017-11-16T23:17:12.6584893-05:00"}}&jsresp[hash]=bgd1e0Cxhj5s1FQaUFFYk7BMnSIl4Ez1jPMopZFp+4MyN9chFZZoo/3IuZPX7bbQ+RyaReKN1CNJXxRmjnLMRQ==
So your $_REQUEST['jsresp'] is an array
you need to json_decode the 'response' index of the array :
$respObj = json_decode($_REQUEST['jsresp']['response']);
$hash = $_REQUEST['jsresp']['hash'];
And then you can go on...
First i will advice you to look at the value in your #resp_holder, if the value there is correct as you want it, then you can post it using anyhow you want it, json is a string, and as long as that string conforms to json standard, and they are no special characters in the it like &, then you can decode it in your php.
You can use html or json dataType to post it.
Allow me to preface this by saying that I looked at multiple SO posts on this and I am still lost.
So in my php code I am fetching data from my database and then I am trying to insert it into an array as follows:
$arrayResult = array();
foreach ($result as $item) {
array_push($arrayResult, array("type" => $item['type'],
"count" => $item['count'])
);
}
echo json_encode($arrayResult);
My problem is as follows, the only time my JS shows any data is when I just print out the data on a successful AJAX call, any attempts at manipulating it fail totally. As in, no data shown at all.
var arrayResult = null;
$.get("../php/displayGraph.php",
function (data) {
arrayResult = (data);
var result = JSON.parse(arrayResult);
$("#results").html(arrayResult);
//$("#results").html(JSON.parse(arrayResult));
}
);
The result of this is:
[{"type":"Entertainment","count":"4"},{"type":"Other","count":"31"},{"type":"Politics","count":"50"},{"type":"Sports","count":"3"},{"type":"Technology","count":"9"}]
I am honestly at a loss in terms of what I even need to do to make it work. And here I thought java was bad with json.
Try like this,
$.get("../php/displayGraph.php",
function (data) {
$.each(data, function (i,item){
console.log(item.type + " === " +item.count);
}
/*arrayResult = (data);
var result = JSON.parse(arrayResult);*/
//$("#results").html(arrayResult);
//$("#results").html(JSON.parse(arrayResult));
}
);
Not sure why, but the following works
$.get("../php/displayGraph.php",
function (data) {
var result = JSON.parse(data);
$("#results").html(data);
console.log(result[1][0].count);
}
);
Certainly is a 2D array the way my php makes it, but i did not think this would be how to do as all the other tutorials i saw never had it like this.
everybody. I have an AJAX call which returns me an error mentioned in the title. I believe this is the line which causes error: var obj = jQuery.parseJSON(data); Maybe I wrongly constructed userData.
This is my jQuery:
var userData = 'email=' + email + '&password=' + password;
$.ajax({
type: 'POST',
url: './api/getInfo.php',
data: userData,
success: function(data){
var obj = jQuery.parseJSON(data);
$('#name').html(obj.firstName + ' ' + obj.lastName);
...
},
error: function(){
alert('ERROR');
}
});
And this is getInfo.php:
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
$email = prepareInput($_POST['email']);
$password = prepareInput($_POST['password']);
$stmt = $connection->conn->prepare('SELECT firstName,lastName,... FROM tb_users WHERE email = ? AND password = ?');
$stmt->bind_param('ss',$email,$password);
$stmt->execute();
$result = $stmt->get_result();
$obj = $result->fetch_assoc();
echo json_encode($obj);
}
Can someone tell me if I'm doing something wrong?
UPDATE
function prepareInput($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
Data passed from PHP is empty even if $obj contains values (I checked this by echoing them) so it must be the problem with echo json_encode($obj); statement.
SOLUTION
I finally found the answer - Link. It was encoding problem. If some strings are not UTF-8 json_encode() will return empty string so to deal with this you need to convert these strings to UTF-8.
Since you mention: the success function does not receive any data
It seems the server response is empty, because (a) the credentials are wrong or (b) there is an error in PHP and your code never reaches the echo line.
Add some error handling / confirmation in PHP and make sure that $obj has a value before you return it.
try {
// ... sql code
if ( $obj && is_array( $obj ) ) {
echo json_encode( $obj );
} else {
echo json_encode( array( 'error' => 'wrong username or password' ) );
}
} catch (Exception $ex) {
echo json_encode( array( 'error' => $ex->getMessage() ) );
}
Update; notes for debugging the SQL
To test the SQL:
As first line in the condition (method == POST) add echo json_encode(array('resp'=>'test')); exit; and run the Ajax code.
Your console.log() should now display the {resp:test} JSON object. This way you know that your ajax call actually reaches the part with the SQL. If you still do not get any output then something else is wrong in your code...
First use a hardcoded SQL: Just enter email/password in WHERE that you know will give you a result. Do not use ->bind_param() or $_POST data, just execute a plain SQL statement and see if AJAX returns a value.
If Ajax works now, then modify the hardcoded SQL to move the static email/password string into bind_param(). Still not using $_POST data now, but we check if the bind_param method works. Again check if Ajax is still working.
If still working, then use direkt $_POST data in the bind_param() call, like ->bind_param('ss', $_POST['email'], $_POST['password']) - if this is working, then you know that there's a problem with the prepareInput() function.
When testing each step, you should quickly find out, which part is not working.
You don't seem to check if your execute is a success before you use the result.
If I was you I will try to console.log(data) before use jsonParse. I think you will find some surprise.
Also, check your apache logs. Maybe PHP throw you some warnings in it.
My PHP script is having a problem decoding the JSON that I'm sending to it via AJAX.
The jQuery:
$.ajax({
url : 'admin/modifyPermissions',
type : 'post',
data : {
'JSON' : JSON
},
success : function(msg){
if(msg == '1') {
alert('Permissions saved successfully');
} else {
alert(msg);
}
}
});
The PHP script:
public function modifyPermissions(){
if(isset($_POST['JSON']) && !empty($_POST['JSON'])) {
$json = json_decode($_POST['JSON'],true);
if($json !== NULL && $json !== FALSE) {
} elseif($json === NULL){
die('The string passed is not a valid JSON object and cannot be decoded.' . "\n" . $_POST['JSON']);
} else {
die('There was an error with the JSON string');
}
} else {
die('No JSON string was sent!');
}
}
The JSON that gets passed looks well formed to me:
{"martin":{3,5},"user2":{3,4,5}}
And PHP is returning null. I have PHP 5.2.7 installed on my server, so I can't use json_last_error()
{"martin":{3,5},"user2":{3,4,5}}
Not valid JSON. Valid JSON may look like this:
{"martin":[3,5],"user2":[3,4,5]}
You're not sending valid JSON, thus the error. Look at the comment #Matt added.
So that you won't reproduce the same error, before sending it over to PHP, don't try to make your own JSON string, use what JS offers you. Example:
var obj = { key: val, okey: oval }
objJSON = JSON.stringify(obj)
// objJSON is ALWAYS going to be valid json
Your JSON is invalid.
The {} notation denotes key/value pairs, where as you're using it as an array.
Your JSON should be,
{"martin":[3,5],"user2":[3,4,5]}
I am trying to create a little ajax chat system (just for the heck of it) and I am using prototype.js to handle the ajax part.
One thing I have read in the help is that if you return json data, the callback function will fill that json data in the second parameter.
So in my php file that gets called I have:
header('Content-type: application/json');
if (($response = $acs_ajch_sql->postmsg($acs_ajch_msg,$acs_ajch_username,$acs_ajch_channel,$acs_ajch_ts_client)) === true)
echo json_encode(array('lastid' => $acs_ajch_sql->msgid));
else
echo json_encode(array('error' => $response));
On the ajax request I have:
onSuccess: function (response,json) {
alert(response.responseText);
alert(json);
}
The alert of the response.responseText gives me {"lastid": 8 } but the json gives me null.
Anyone know how I can make this work?
This is the correct syntax for retrieving JSON with Prototype
onSuccess: function(response){
var json = response.responseText.evalJSON();
}
There is a property of Response: Response.responseJSON which is filled with a JSON objects only if the backend returns Content-Type: application/json, i.e. if you do something like this in your backend code:
$this->output->set_content_type('application/json');
$this->output->set_output(json_encode($answer));
//this is within a Codeigniter controller
in this case Response.responseJSON != undefined which you can check on the receiving end, in your onSuccess(t) handler:
onSuccess:function(t) {
if (t.responseJSON != undefined)
{
// backend sent some JSON content (maybe with error messages?)
}
else
{
// backend sent some text/html, let's say content for my target DIV
}
}
I am not really answering the question about the second parameter of the handler, but if it does exist, for sure Prototype will only provide it in case of proper content type of the response.
This comes from Prototype official :
Evaluating a JavaScript response
Sometimes the application is designed
to send JavaScript code as a response.
If the content type of the response
matches the MIME type of JavaScript
then this is true and Prototype will
automatically eval() returned code.
You don't need to handle the response
explicitly if you don't need to.
Alternatively, if the response holds a
X-JSON header, its content will be
parsed, saved as an object and sent to
the callbacks as the second argument:
new Ajax.Request('/some_url', {
method:'get', onSuccess:
function(transport, json){
alert(json ? Object.inspect(json) : "no JSON object");
}
});
Use this functionality when you want to fetch non-trivial
data with Ajax but want to avoid the
overhead of parsing XML responses.
JSON is much faster (and lighter) than
XML.
You could also just skip the framework. Here's a cross-browser compatible way to do ajax, used in a comments widget:
//fetches comments from the server
CommentWidget.prototype.getComments = function() {
var commentURL = this.getCommentsURL + this.obj.type + '/' + this.obj.id;
this.asyncRequest('GET', commentURL, null);
}
//initiates an XHR request
CommentWidget.prototype.asyncRequest = function(method, uri, form) {
var o = createXhrObject()
if(!o) { return null; }
o.open(method, uri, true);
o.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
var self = this;
o.onreadystatechange = function () {self.callback(o)};
if (form) {
o.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
o.send(makePostData(form));
} else {
o.send('');
}
}
//after a comment is posted, this rewrites the comments on the page
CommentWidget.prototype.callback = function(o) {
if (o.readyState != 4) { return }
//turns the JSON string into a JavaScript object.
var response_obj = eval('(' + o.responseText + ')');
this.comments = response_obj.comments;
this.refresh()
}
I open-sourced this code here http://www.trailbehind.com/comment_widget