views:

182

answers:

2
 <script type="text/javascript">
        $(function () {
            $("select#oblast").change(function () {
                var oblast_id = $("#oblast > option:selected").attr("value");
                $("#Rayondiv").hide();
                $.ajax({
                    type: "GET",
                    contentType: "application/json",
                    url: "http://site.com/Regions.aspx/FindGorodByOblastID/",
                    data: 'oblast_id=' + oblast_id,
                    dataType: "json",
                    success: function (data) {                       
                        if (data.length > 0) {

                            var options = '';
                            for (p in data) {
                                var gorod = data[p];
                                options += "<option value='" + gorod.Id + "'>" + gorod.Name + "</option>";
                            }
                            $("#gorod").removeAttr('disabled').html(options);

                        } else {

                            $("#gorod").attr('disabled', false).html('');
                        }
                    }
                });
            });
        });
</script>
A: 

Where is this code running? Unless you're on http://site.com/, that won't work for security reasons.

If that's the case, is there any way you make the request and do whatever server side?

Maybe make the request to some page you set up on your site, and in its code behind do the work:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://example.com");
request.Method = "GET";
request.Headers["Accept-Encoding"] = "gzip,deflate";

HttpWebResponse response = (HttpWebResponse)request.GetResponse();
String html = new StreamReader(response.GetResponseStream()).ReadToEnd();
response.Close();
Matt Blaine
Yeah the domain you're running in is key here - IE won't allow cross-domain GETs as far as I know
Jaco Pretorius
+1  A: 

If you're trying to call a URL on a third-party site you will need to look in to JSONP (JSON with Padding) options. These are designed to make it slightly easier to work with third-party services.

See jQuery.ajax and the discussion of the "jsonp" in there for some additional details.

Zhaph - Ben Duguid