To connect to a php backend script:
var url = baseUrl + "jsonScript.php";
var request = [CPURLRequest requestWithURL: url];
var data = [CPURLConnection sendSynchronousRequest: request
returningResponse:nil error:nil];
var str = [data description];
'str' will contain a JSON object or array that you will need to
decode. I hope somebody adds a method to CPData to convert a JSON
object to a CPArray or CPDictionary.
To send data back to a php backend script using POST:
var url = baseUrl + "postScript.php";
var request = [CPURLRequest requestWithURL: url];
var body = "field1=" + field1Value + "&field2=" + field2Value +
"&field3=" + field3Value;
[request setHTTPMethod: "POST"];
[request setValue:"application/x-www-form-urlencoded"
forHTTPHeaderField:"Content-Type"];
[request setValue:[body length] forHTTPHeaderField:"Content-Length"];
[request setHTTPBody: body];
var data = [CPURLConnection sendSynchronousRequest: request
returningResponse:nil error:nil];
'data' contains any results sent back from the script that you can use
to validate the results of the operation.
This are synchronous connections. I haven't tried with asynchronous
yet, but I'll be playing with them soon. I'm pretty sure there are
better ways to handle the connection to the backend, I'm not an
expert. I just wanted to share some code that might help you.
Eisen
Eisen
Sent from my iPhone 3G
You should use CPURLConnection (a thin wrapper around XMLHttpRequest)
and parse the result using CPJSObjectCreateFromJSON() (essentially
"eval" with checks to make sure the input is valid JSON)
You won't need to do the JSONP style wrapping with this method.
(btw the reason your posts didn't show up is posts by new members need
to be approved. We should turn that off though)
Sent from my iPhone
On Sep 5, 2008, at 5:44 AM, "alx...@googlemail.com" <alx...@googlemail.com