I am trying to send an array of dictionaries to server and before that I am converting to json format
NOTE:- the server is working well my friend in android is getting the response and data for successful post method
below is my code of post method:-
NSError *error;
NSMutableDictionary *tvarns = [[NSMutableDictionary alloc] init];
tvarns[@"order_cart"]=_order_cart;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:tvarns
options:NSJSONWritingPrettyPrinted error:&error];
NSLog(@"%@",jsonData);
NSString* order_cart;
order_cart = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSLog(@"%@",order_cart);
NSString *post = [NSString stringWithFormat:@"%@",order_cart];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
// Next up, we read the postData's length, so we can pass it along in the request.
NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];
// Now that we have what we'd like to post, we can create an NSMutableURLRequest, and include our postData
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:@"http://test.kre8tives.com/barebon/add_cartapi.php"]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:postData];
NSLog(@"the data Details is %@", post);
// And finally, we can send our request, and read the reply by creating a new NSURLSession:
NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
[[session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSString *requestReply = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]; // this is json string
NSLog(@"Reply = %@", requestReply);
// NSError *error;
NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error]; // you need to convert to dictionary object
NSLog(@"requestReply: %@", jsonDict);
}] resume];
[self presentViewController:alertController animated:YES completion:nil];
}
The value which the server wants is
{"order_cart":[{"customer_id":"114","item_id":14,"_id":"591d3822f55e2270202d8ff1","item_name":"Bira White","item_quantity":"2","price":"180.0","qr_code":"table21","phone":"7008841769","customer_name":"Samanthaa"},{"customer_id":"114","item_id":15,"_id":"591d3822f55e2270202d8ff0","item_name":"Bira Blonde","item_quantity":"2","price":"200.0","qr_code":"table21","phone":"7008841769","customer_name":"Samanthaa"}]}
My nslog is
2017-06-28 16:16:41.334 Barebones[3139:211622] the data Details is {
"order_cart" : [
{
"phone" : "9047038606",
"item_quantity" : "7",
"qr_code" : "table21",
"item_name" : "Bottled Water",
"price" : "175",
"customer_id" : "116",
"customer_name" : "Akshay"
},
{
"phone" : "9047038606",
"item_quantity" : "2",
"qr_code" : "table21",
"item_name" : "Fresh Lime Soda(Sweet, Salt, Plain)",
"price" : "158",
"customer_id" : "116",
"customer_name" : "Akshay"
},
{
"phone" : "9047038606",
"item_quantity" : "2",
"qr_code" : "table21",
"item_name" : "Bottled Water",
"price" : "50",
"customer_id" : "116",
"customer_name" : "Akshay"
},
{
"phone" : "9047038606",
"item_quantity" : "2",
"_id" : "591d3822f55e2270202d8ff0",
"item_name" : "Bira Blonde",
"price" : "86",
"qr_code" : "table21",
"customer_id" : "116",
"item_id" : "3971",
"customer_name" : "Akshay"
},
{
"phone" : "9047038606",
"item_quantity" : "1",
"qr_code" : "table21",
"item_name" : "Bottled Water",
"price" : "25",
"customer_id" : "116",
"customer_name" : "Akshay"
}
]
}
2017-06-28 16:16:41.485 Barebones[3139:211669] Reply = {"order_id":3961,"success":1}
<script type='text/javascript' src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js'>. </script>
<script>
var data_array='{"source":{"name":"Kre8Tives","id":"SJQ6kyA1","order_id":"3961"},"customer":{"firstname":"","mobile":""},"tabType":"table","tabId":"591d3790498782970be66302","tableNumber":"1","items":null}';
alert(data_array);
var settings = {
'async': true,
'crossDomain': true,
'url': 'http://posistapi.com/api/v1/table_order/push?customer_key=8c982f5e9ef091e9d5c8fb142311b49a68049b102de5e0987ebce29755c454227fbc7573c7da4199f126524a3ac7a39a&tabtype=table',
'method': 'POST',
'headers': {
'postman-token': 'e200ae19-d083-a98a-6db3-53fc2f8bd73f',
'cache-control': 'no-cache',
'authorization': 'Basic [removed]',
'content-type': 'application/json'
},
'processData': false,
'data': data_array,
'acceptUrl':'http://test.kre8tives.com/barebon/order_confirm.php',
'rejectUrl':'http://test.kre8tives.com/barebon/order_confirm.php',
'billPrintUrl':'http://test.kre8tives.com/barebon/order_confirm.php'
}
$.ajax(settings).done(function (response) {
console.log(response);
});
</script>
2017-06-28 16:16:41.486 Barebones[3139:211669] requestReply: (null)
I have converted to desired son format but still I am getting no reply from server
Even if I send return the data what I send from server I get null!!why is that ???thanks in advance !!:)
This is my PHP code
<?php
include('order-function.php');
if($_SERVER["REQUEST_METHOD"] == "POST")
{
$order_cart=$_POST['order_cart'];
$items=json_decode($order_cart,true);
include('dbconfig.php');
$response = array();
try
{
$total_item_price=0;
foreach ($items['order_cart'] as $item) { //['order_cart']
$customer_id=$item['customer_id'];
$customer_name=$item['customer_name'];
$phone=$item['phone'];
$_id=$item['_id'];
$item_name=$item['item_name'];
$item_quantity=$item['item_quantity'];
$price=$item['price'];
$qr_code=$item['qr_code'];
$products[]=array(
"_id"=>"$_id",
"item_name"=> $item_name,
"item_price"=> $price,
"item_quantity"=> $item_quantity
);
$price_new+=$price;
$item_push[]=array(
"id"=>"$_id",
"rate"=> $price,
"quantity"=> $item_quantity
);
}
$product_details=json_encode($products);
$add_to_cart = "INSERT INTO order_cart (customer_id,product_details,order_amount,qr_code) VALUES ('$customer_id','$product_details','$price_new','$qr_code')";
$result = mysqli_query($conn,$add_to_cart);
$order_id=mysqli_insert_id($conn);
if ($result) {
$data['source']=array("name"=>"Kre8Tives",
"id"=>"SJQ6kyA1",
"order_id"=>"$order_id"
);
$data['customer']=array(
"firstname"=>"$customer_name",
"mobile"=>"$phone"
);
$data['tabType']= "table";
$data['tabId']= "591d3790498782970be66302";
$data['tableNumber']= "1";
//items
$data_one=json_encode($item_push,JSON_NUMERIC_CHECK);
$data_new=json_decode($data_one);
$data['items']=$data_new;
$data_item=json_encode($data);
$response["order_id"] = $order_id;
$response["success"] = 1;
// print_r(json_encode($data));die;
} else {
$response["success"] = 0;
}
echo json_encode($response);
push_order($data_item);
}
catch(Exception $e)
{
$response["success"] = 0;
$response["message"] = "error occured";
}
}
?>