POST request in Windows phone - windows-phone-7

I am trying to get some data from the user and send it to my server for logging purposes. I was able to do it easily with jquery for my website. When I try to write a windows app for the same I am clueless as to how to post the data to my server.
So far I have fetched the data from the user and I am trying to use OpenWriteAsync function of webclient to send it to my server. I understand how OpenWriteAsync function works but I am not able to figure out how to send my own data.
My php script in my server will get values with name "FILENAME" and "FTIME" like,
$fname = $_POST['FILENAME'];
$time = $_POST['FTIME'];
So I have got this information from the user in two different string variables in c# inside my app. Now how do I send this to my server so that my php script gets this value and logs it inside my server.
I hope I am clear.

Take a look at UploadData method.
The way you use it something like this:
UploadData("http://abc.com","POST", "data in byte array");
Note: you need to convert data to byte array
Reference: http://msdn.microsoft.com/en-us/library/ktfa4fek(v=VS.90).aspx

You can do this by using the ContentType property in the HTTPWebRequest object.

Related

How to get json data from Server with jquery mobile?

I am trying to get the Bitcoin course from a web server.
Then we try it with a JSON from local, it works.
In Firebug, I can see the get request to bitcoincharts.com, but there is no answer.
What's wrong with my code?
$('#LitecoinMenue').append('<p><b>Litecoin: 42</b></p>');
$.getJSON('http://api.bitcoincharts.com/v1/weighted_prices.json',
function(data){
$.each(data.USD, function(index,item){
$('#BitcoinMenue').append('<p><b>Bitcoin:'+ item+'</b></p>');
});
});
The reason your code doesn't work is because of a rule called Same-origin policy. This rule requires that all AJAX requests are made to a file on the same domain name. It is not possible to use $.getJSON, or any other AJAX function to load a file from an external domain.
There are only a few options available, the most common is to create a PHP file to act as a proxy, and store it on the same domain. For example:
proxy.php
<?php
$url = base64_decode($_GET['url']);
return file_get_contents($url);
?>
Your page above
$('#LitecoinMenue').append('<p><b>Litecoin: 42</b></p>');
$.getJSON('proxy.php?url=aHR0cDovL2FwaS5iaXRjb2luY2hhcnRzLmNvbS92MS93ZWlnaHRlZF9wcmljZXMuanNvbg==',
function(data){
$.each(data.USD, function(index,item){
$('#BitcoinMenue').append('<p><b>Bitcoin:'+ item+'</b></p>');
});
});
Important Notes:
This is just an example. In a real life situation you would probably want to use cURL to get your file. You should also ensure that it is secured so that someone cannot use Firebug to send an AJAX request to fetch a big file (like a movie) or your server could crash.
As you can see, the URL is base64 encoded. This is to ensure that it gets processed correctly as sometimes there are issues when passing an unencoded URL as a GET parameter. You can encode and decode base64 strings with these online converters: http://base64encode.org and http://base64decode.org, or you can use the built in PHP functions base64_encode() and base64_decode().

PHP Kohana 3.2.2 multipart form $_POST not set on MAC but works on Win

I've just encountered a weird problem. I've recently developed a medium size website using Kohana 3.2.2 + jquery + html + WAMP on Windows 7 platform. And everything seems to be working fine, until someone tries accesing the page from Mac platform. It seems that when sending some data with files in multipart form the global variable $_POST is not set, even though when debugging the data in web browser i'm able to see that DATA IS SET :| it's just not accesable by the controller with any $_POST or request->post(). I'm repeating, everything works perfectly when user is accesing page from Windows platform (tested on few separated clients), but not working when accesing from Mac platforms (tested on few separated clients).
It's killing me...
Example of what im trying to do:
In View:
user puts data into inputs (text and file types). Data is being send by form with enctype = multipart/form-data to controller's action
In controller:
$post = request->post();
if($post['sometextinput'] != '') throws exception of unknown index 'sometextinput'.
That's extremely odd. I use Kohana on a daily basis (I develop on a Mac) and have never had an issue like that. Could you post your controller and the view? I'll plug it into a dummy project and see if I can replicate the issue. If I can I'll do what I can to get it working.
EDIT:
Could it possible be an odd configuration issue?
Just for my own clarification.
You're submitting a form that contains input fields and one or more file uploads.
When viewing it on a Windows machine you can see that the data is set in $_POST or $request->post().
On OSX it's not viewable to the controller via $_POST or $request->post();
In your before method make sure you have "parent::before();". If you are already calling parent::before() try putting it as the first statement in your before() method. If that doesn't work try adding it as the last statement. It's a shot in the dark, but it's worth a try.
If you don't have a before() method then add one and call parent::before();.
I'm not sure if you were just in a hurry to type up your example above but it actually should be:
#$post = request->post(); //wouldn't recommend doing this
if($this->request->post('sometextinput') != '') throws exception of unknown index 'sometextinput'.

Retrieving JSON from an external API with backbone

I'm new to backbone.js and I've read other solutions to similar problems but still can't get my example to work. I have a basic rails api that is returning some JSON from the url below and I am trying to access in through a backbone.js front end. Since they are one different servers I think I need to use a 'jsonp' request. I'm currently doing this by overriding the sync function in my backbone collection.
Api url:
http://guarded-wave-4073.herokuapp.com/api/v1/plans.json
sync: function(method, model, options) {
options.timeout = 10000;
options.dataType = 'jsonp';
options.url = 'http://guarded-wave-4073.herokuapp.com/api/v1/plans.json'
return Backbone.sync(method, model, options);
}
To test this I create a new 'plans' collection in my chrome console using "plans = new Plans()" and then "plans.fetch()" to try and get the JSON.
When I call plans.models afterwards I still have an empty array and the object that returns from plans.fetch() doesn't seem to have any json data included.
Any ideas where I'm going wrong?
I have had the same problem before. You should not have to override your sync method.
Taken from Stackoverflow Answer
"The JSONP technique uses a completely different mechanism for issuing HTTP requests to a server and acting on the response. It requires cooperating code in the client page and on the server. The server must have a URL that responds to HTTP "GET" requests with a block of JSON wrapped in a function call. Thus, you can't just do JSONP transactions to any old server; it must be a server that explicitly provides the functionality."
Are you sure your server abides to the above? Test with another compatible jsonp service (Twitter) to see if you receive results?
Have you tried overriding the fetch method as well?
You should add ?callback=? to your api url in order to enable jsonp

Cross domain javascript ajax request - status 200 OK but no response

Here is my situation:
Im creating a widget that site admins can embed in their site and the data are stored in my server. So the script basically has to make an ajax request to a php file in my server to update the database. Right? Right :)
The ajax request works excellent when i run it in my local server but it does not work when the php file is on my ONLINE server.
This is the code im using:
var url = "http://www.mydomain.net/ajax_php.php";
var params = "com=ins&id=1&mail=mymail#site.net";
http.async = true;
http.open("POST", url, true);
http.onreadystatechange = function() {
if(http.readyState == 4 && http.status == 200) {
//do my things here
alert( http.responseText );
}
}
http.send(params);
In firebug it shows: http://www.mydomain.net/ajax_php.php 200 OK X 600ms.
When i check the ajax responnseText I always get a Status:0
Now my question is: "Can i do cross-domain ajax requests by default? Might this be a cross-domain ajax problem? Since it works when the requested file resides in my local server but DOESN'T work when the requested file is in another server, im thinking ajax requests to another remote server might be denied? Can you help me clear on this?
Thanks..
Cross-domain requests are not directly allowed. However, there is a commonly-used technique called JSONP that will allow you to avoid this restriction through the use of script tags. Basically, you create a callback function with a known name:
function receiveData(data) {
// ...
}
And then your server wraps JSON data in a function call, like this:
receiveData({"the": "data"});
And you "call" the cross-domain server by adding a script tag to your page. jQuery elegantly wraps all of this up in its ajax function.
Another technique that I've had to use at times is cross-document communication through iframes. You can have one window talk to another, even cross-domain, in a restricted manner through postMessage. Note that only recent browsers have this functionality, so that option is not viable in all cases without resorting to hackery.
You're going to need to have your response sent back to your client via a JSONP call.
What you'll need to do is to have your request for data wrapped in a script tag. Your server will respond with your data wrapped in a function call. By downloading the script as an external resource, your browser will execute the script (just like adding a reference to an external JS file like jQuery) and pass the data to a known JS method. Your JS method will then take the data and do whatever you need to do with it.
Lots of steps involved. Using a library like jQuery provides a lot of support for this.
Hope this helps.

Can you retrieve your Skype status using JSONP?

Does anyone know of a URL to get your Skype status using JSONP?
I've only found an XML status URL so far (http://mystatus.skype.com/username.xml).
(I'm trying to query Skype using AJAX. Yes, I could use a server-side proxy script to beat the cross-domain limits, but a direct call would be awesome.)
Simon.
Well apparently you can get a text-only version of the status by changing the extension to .txt:
http://mystatus.skype.com/username.txt
It will return "Online" or "Offline". About the Cross-domain AJAX, you can only do it via server and direct call is definitely not allowed.
You might change the headline to 'JSONP' instead of JSON. That's what you want.
JSONP hijacks cross domain fetches like this to work, without server proxies, by carrying the data in fetches. It's like the most hackish useful technology I come to mind, right now. :)
I nagged Skype about this - the easiest way out would be for their servers to have an official, documented JSONP interface. I hope they'll do that.
In the mean time, this is how I got the problem solved:
Placed this PHP script on my server, alongside the usual HTML: http://benalman.com/projects/php-simple-proxy/
Edited the configuration of it like so:
$enable_native = true;
$valid_url_regex = '/^http:\/\/mystatus\.skype\.com\/myuserid.*/';
This allows it to fetch (via curl running on the server) the mystatus.skype.com/myuserid.num (or .txt) information.
Fetching from JS with URL:
ba-simple-proxy.php?url=http%3A%2F%2Fmystatus.skype.com%2Fmyuserid.num&mode=native&full_status=1
That's it. Pheeew... :)
Also you can retrieve it using PHP
function getSkypeStatus($username) {
$data = file_get_contents('http://mystatus.skype.com/' . urlencode($username) . '.xml');
return strpos($data, '<presence xml:lang="en">Offline</presence>') ? 'Offline' : 'Online';
}
OR
function getSkypeStatus($username) {
$data = file_get_contents('http://mystatus.skype.com/' . urlencode($username) . '.xml');
preg_match('#<presence xml:lang="en">(.*?)</presence>#i', $data, $match);
return isset($match[1]) ? $match[1] : 'Error retrieving status';
}
Cheers!
Thanks to Bradgrafelman from - http://www.phpbuilder.com/board/showthread.php?t=10361050

Resources