NancyFx and angularjs pass init params using $http.get

时间:2016-10-20 19:32:22

标签: angularjs nancy

On initial load of the page, I am trying to pass a start and end variables to Nancy using an $http.get method like so:

  $http.get("/api/sample", {params:{"start": start, "end": end}}).then(function(response) ....

On nancy side, I am not sure how to get my parameters start and end.. so this is the question... How can I get my parameters I passed

 Get["/api/sample"] = p =>
  {
  var data = p.data;
  // also tried various suggestions and this this.Bind<dynamic>();
  sampleData= Data(data);
  return sampleData;
  };

After this, I have a view like so:

Get["/testview"] = _ => View["/testview"];

and I would like to get my data passed as json like so:

Get["/api/sample"] = p => Response.AsJson(sampleData);

at the end my sampleData will be shown on my testview.

3 个答案:

答案 0 :(得分:1)

You are making a big mistake here.

POST with data: This is probably what you want. If you are passing data along, that probably means you are modifying some model or performing some action on the server. These types of actions are typically done with POST requests.

GET with query string data: You can convert your data to query string parameters and pass them along to the server that way.

url: '/api/sample?ids=1,2,3'

答案 1 :(得分:1)

你可以这样做:

Get["/api/sample/{start:int}/{end:int}"] = p =>
{
  var data = FetchData(p.start,p.end);
  return View("testView",data);
}

有关详细信息,请查看Wiki

答案 2 :(得分:0)

我只是将其添加为替代品。

  Get["/api/sample"] = p =>
  {
     var myData = this.Bind<SomeModel>();
     ....
  }

希望这有助于其他人。