1

私は MVC 4 を使用して JQuery を使用しGETPOST要求していますが、JQuery で要求を送信したり、サーバーから応答を取得したりできないようです。

<script type="text/javascript">
    function search() {
        var lat = $('#latitude').val();
        var long = $('#longitude').val();

        $.ajax({
            url: "api/Attractions/?longitude=" + long + "&latitude=" + lat,
            type: "GET",
            success: function (data) {
                if (data == null) {
                    $('#attractionName').html("No attractions to search");
                }
                else {
                    $('#attractionName').html("You should visit " + data.Name);
                    displayMap(data.Location.Geography.WellKnownText, data.Name);
                }
            }
        });
    }
    // Get Geo Information
    function displayMap(coordinateString, name) {
        // WellKnownText is in format 'POINT (<longitude>, <latitude>)'
        coordinateString = coordinateString.replace("POINT (", "").replace(")", "");
        var long = coordinateString.substring(0, coordinateString.indexOf(" "));
        var lat = coordinateString.substring(coordinateString.indexOf(" ") + 1);

        // Show map centered on nearest attraction
        var map = new VEMap('myMap');
        map.LoadMap(new VELatLong(lat, long), 15, VEMapStyle.Aerial);

        // Add a pin for the attraction
        var pin = new VEShape(VEShapeType.Pushpin, new VELatLong(lat, long));
        pin.SetTitle(name);
        map.AddShape(pin);
    }
</script>

次に、ボタンのコード:

<h1>Find the Closest Tourist Attraction</h1>
<div>
    <label for="longitude">Longitude:</label>
    <input type="text" id="longitude" size="10" />
    <label for="latitude">Latitude:</label>
    <input type="text" id="latitude" size="10" />
    <input type="button" value="Search" onclick="search();" />
</div>
4

1 に答える 1

0

You should be using the URL helper. The url in the ajax call should look somewhat like this, I havent tested it but I know for a fact that I had to do this to fix the issue.

url:"@Url.Action("action", "controller")" + "?longitude=" + etc..
于 2012-10-20T19:26:40.743 に答える