|
 |
|
มีปัญหาในการใช้ PHP เรียกข้อมูล Web Service จาก ASP.net ครับ |
|
 |
|
|
 |
 |
|
ผมได้ทำ ชุดข้อมูล JSON โดยใช้ php โดยค่าที่ได้ออกมาเป็นแบบนี้ครับ
Code
[{"0":"BOXING","ClassID":"BOXING","1":"Boxing","ClassName":"Boxing","2":"Boxing class","ClassDetail":"Boxing class","3":"http:\/\/www.revones.com\/class\/booking\/content\/bookingclass\/BOXING.jpg","ClassPicPath":"http:\/\/www.revones.com\/class\/booking\/content\/bookingclass\/BOXING.jpg","4":"","ClassSession":"","5":"0","ClassVolum":"0","6":"","InstructorID":""},{"0":"GROUPX","ClassID":"GROUPX","1":"Group-X","ClassName":"Group-X","2":"Group-X class","ClassDetail":"Group-X class","3":"http:\/\/www.revones.com\/class\/booking\/content\/bookingclass\/GROUPX.jpg","ClassPicPath":"http:\/\/www.revones.com\/class\/booking\/content\/bookingclass\/GROUPX.jpg","4":"","ClassSession":"","5":"0","ClassVolum":"0","6":"","InstructorID":""},{"0":"YOGA","ClassID":"YOGA","1":"Yoga","ClassName":"Yoga","2":"Yoga class","ClassDetail":"Yoga class","3":"http:\/\/www.revones.com\/class\/booking\/content\/bookingclass\/YOGA.jpg","ClassPicPath":"http:\/\/www.revones.com\/class\/booking\/content\/bookingclass\/YOGA.jpg","4":"","ClassSession":"","5":"0","ClassVolum":"0","6":"","InstructorID":""},{"0":"PILATES","ClassID":"PILATES","1":"Pilates","ClassName":"Pilates","2":"Pilates class","ClassDetail":"Pilates class","3":"http:\/\/www.revones.com\/class\/booking\/content\/bookingclass\/PILATES.jpg","ClassPicPath":"http:\/\/www.revones.com\/class\/booking\/content\/bookingclass\/PILATES.jpg","4":"","ClassSession":"","5":"0","ClassVolum":"0","6":"","InstructorID":""},{"0":"FITNESS","ClassID":"FITNESS","1":"Fitness","ClassName":"Fitness","2":"Fitness class","ClassDetail":"Fitness class","3":"http:\/\/www.revones.com\/class\/booking\/content\/bookingclass\/FITNESS.jpg","ClassPicPath":"http:\/\/www.revones.com\/class\/booking\/content\/bookingclass\/FITNESS.jpg","4":"","ClassSession":"","5":"0","ClassVolum":"0","6":"","InstructorID":""}]
แต่พอใช้ ASP.net ในการทำ JSON Web service กลับได้ return ที่ไม่เหมือนกันครับ

Code
[ { "ClassID": "BOXING", "ClassName": "Boxing", "ClassDetail": "Boxing class", "ClassPicPath": "http://www.revones.com/class/booking/content/bookingclass/BOXING.jpg", "ClassSession": "", "ClassVolum": "0", "InstructorID": "" }, { "ClassID": "FITNESS", "ClassName": "Fitness", "ClassDetail": "Fitness class", "ClassPicPath": "http://www.revones.com/class/booking/content/bookingclass/FITNESS.jpg", "ClassSession": "", "ClassVolum": "0", "InstructorID": "" }, { "ClassID": "GROUPX", "ClassName": "Group-X", "ClassDetail": "Group-X class", "ClassPicPath": "http://www.revones.com/class/booking/content/bookingclass/GROUPX.jpg", "ClassSession": "", "ClassVolum": "0", "InstructorID": "" }, { "ClassID": "PILATES", "ClassName": "Pilates", "ClassDetail": "Pilates class", "ClassPicPath": "http://www.revones.com/class/booking/content/bookingclass/PILATES.jpg", "ClassSession": "", "ClassVolum": "0", "InstructorID": "" }, { "ClassID": "YOGA", "ClassName": "Yoga", "ClassDetail": "Yoga class", "ClassPicPath": "http://www.revones.com/class/booking/content/bookingclass/YOGA.jpg", "ClassSession": "", "ClassVolum": "0", "InstructorID": "" } ]
ผมอยากให้ข้อมูลที่ทำจาก ASP.net สามารถ return JSON ออกมาได้เหมือนกับ PHP ด้านบนครับ
อันนี้เป็น Code PHP ที่ทำแล้วข้อมูล JSON ที่ออกมาเป็นอย่างที่ผมต้องการครับ
Code
<?php
header('content-type: application/json; charset=utf-8');
function returnCustomer()
{
$objConnect = mysql_connect("localhost","user","password") or die(mysql_error());
$objDB = mysql_select_db("mydatabase");
$strSQL = "SELECT * FROM mytable";
$objQuery = mysql_query($strSQL) or die (mysql_error());
$resultArray = array();
while($obResult = mysql_fetch_array($objQuery))
{
array_push($resultArray,$obResult);
}
mysql_close($objConnect);
return $resultArray;
}
$data = returnCustomer();
//echo $_GET['callback'] . '('.json_encode($data).')'; //JSONP
echo json_encode($data);//JSON
//echo '{"class":'.json_encode($data).'}';
?>
ส่วน code่ ทางฝั่ง ASP.net ผมไม่มีครับเนื่องจากเป็นฝั่งของอีกบริษัทนึงครับ ซึงผมได้ให้ file .sql เขาไปสร้าง db แล้วให้เขาทำ Webservice ออกมาให้ครับ
ผมอยากถามผู้รู้ว่า JSON ที่ออกมาจาก ASP.net ด้านบนนั้น ผมสามารถนำมาเขียน code ฝั่ง PHP เพิ่มให้ตรงกับที่ผมต้องการได้หรือไม่ครับ
Tag : PHP, MySQL

|
|
 |
 |
 |
 |
Date :
2014-10-05 23:47:24 |
By :
haffalump |
View :
722 |
Reply :
2 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เว้าซื่อๆ เลยนะ
เราว่าเราชอบ format ของ json ฝั่ง asp.net มากกว่าอีก
ดู clean กว่าเยอะเลย
แต่มันก็ดูแปลกๆ อีก มันเหมือนเป็น json ใน xml อีกที
โดยปกติเลยทำ asp.net rest api มันไม่มี tag xml มาครอบแบบนี้นะ
มาเป็น json แบบ cleanๆ เลย
ลองให้ทางฝั่ง asp.net เอา tag xml ออก แก้โค้ดตามข้างล่างดู
https://www.thaicreate.com/dotnet/asp-net-web-service-json.html
|
 |
 |
 |
 |
Date :
2014-10-06 08:11:03 |
By :
ห้ามตอบเกินวันละ 2 กระทู้ |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณมากครับ
พอดี code JSON ที่ผมทำออกมา ผมนำไปใช้ใน Kendo UI PHP น่ะครับ แล้วมันสามารถใช้งานได้
แต่พอมาเป็น format ของ ASP.net มันดันใช้งานไม่ได้ (แต่อาจจะเป็นเพราะ tag xml ก็ได้นะครับ ผมก็ไม่แน่ใจ)
เดี๋ยวคงรอท่านอื่นชี้แนะเพิ่มเติมครับ
|
 |
 |
 |
 |
Date :
2014-10-06 09:13:06 |
By :
Haffalump |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 05
|