【问题标题】:Pass data to web API POST Method将数据传递给 Web API POST 方法
【发布时间】:2019-05-06 15:12:39
【问题描述】:

我是 Javascript 新手,所以这个问题可能是一个基本问题。我正在尝试将数据传递给 Web API 的 POST 方法,但我每次都没有正确接收值。我看到 javascript 有时会发送唯一的第二个响应(即仅 requestOptions2),我认为这是由于回调函数。 (因为它不会等到第一个回调函数完成后才执行第二个回调函数并发布和 AJAX 调用。)另外,我无法从 Microsoft.Maps.loadModule('Microsoft. Maps.Search', function () ) 所以,我正在寻找一种将“点”数据传递给 Web API 而不会丢失信息的方法。

这是我的 Javascript 代码:

function RequestShuttle() {

    Microsoft.Maps.loadModule('Microsoft.Maps.Search', function () {
        var points = [];
        var searchManager = new Microsoft.Maps.Search.SearchManager(map);
        var requestOptions1 = {
            bounds: map.getBounds(),
            where: document.getElementById("origin").value,
            callback: function (answer, userData) {
                map.setView({ bounds: answer.results[0].bestView });
                map.entities.push(new Microsoft.Maps.Pushpin(answer.results[0].location));
                points.push((answer.results[0].location));

            }
        };
        var requestOptions2 = {
            bounds: map.getBounds(),
            where: document.getElementById("destination").value,
            callback: function (answer, userData) {
                map.setView({ bounds: answer.results[0].bestView });
                map.entities.push(new Microsoft.Maps.Pushpin(answer.results[0].location));
                points.push((answer.results[0].location));
                console.log(JSON.stringify(points));
                $.ajax({
                    type: "POST",
                    data: JSON.stringify(points),
                    url: "api/Trip",
                    contentType: "application/json"
                });
            }
        };
        searchManager.geocode(requestOptions1);
        searchManager.geocode(requestOptions2);
    });
}

【问题讨论】:

    标签: javascript c# json ajax post


    【解决方案1】:

    我从this post偷了这个答案

    这可能是构建代码的方式:

    function first() {
       return $.ajax(...);
    }
    
    function second(data, textStatus, jqXHR) {
       return $.ajax(...);
    }
    
    function third(data, textStatus, jqXHR) {
       return $.ajax(...);
    }
    
    function main() {
        first().then(second).then(third);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-22
      • 2013-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多