ASP.NET MVC3 Mapquest jQuery POST请求 [英] ASP.NET MVC3 Mapquest jquery POST request

查看:61
本文介绍了ASP.NET MVC3 Mapquest jQuery POST请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在使用mapquest开发一个地图应用程序,并且为了给他们的服务器打电话,他们建议使用以下URL:
(用于POST请求)
http://www.mapquestapi.com/directions/v1/route?key=YourKeyGoesHere

对于请求,我需要3个参数:
1.我的应用程序密钥
2.表示Source
的位置对象 这可以是type: {street: "SourceCity, SourceState"}的一个JSON对象.
3.与上述目的地相同.

为此,我将代码编写为:

Hi,

Im working on a mapping application using mapquest, and for us to make a call to their server, they recommend the following URL:
(For POST requests)
http://www.mapquestapi.com/directions/v1/route?key=YourKeyGoesHere

For the request, i need 3 parameters:
1. My App Key
2. A location object representing Source
This can be of type: {street: "SourceCity, SourceState"} which is a JSON object.
3. A Destination just as above.

For this purpose, i''ve written the code as:

$(''#submit'').click(function () {

           var appKey = ''MyAppKey'';
           var source = { street: "Brookings, SD" };
           var destination = { street: "Sioux Falls, SD" };
           var inputFormat = ''json'';

           var completeRequest = { key: appKey, locations: [source, destination], inFormat: inputFormat };

           $.ajax({
               url: ''http://www.mapquestapi.com/directions/v1/route?key=MyAppKey'',
               type: ''POST'',
               data: JSON.stringify(completeRequest),
               dataType: ''json'',
               processData: false,
               contentType: "application/json; charset=utf-8",
               success: function (response) {
                   $(''#feedback'').html(response);
               },
               error: function (response) {
                   alert("An Error occured when request was made" + response.toString());
               }

           });
       });



条件是,请求需要有一个名为"locations"的参数,该参数的格式应为locations:[{LocationObjSource},{LocationObjDestination}].

我还使用JSON.stringify()发出正确格式化为JSON的请求.

以上所有代码都是ASP.NET MVC3应用程序中Razor视图的一部分.

每当请求触发时,我都会得到错误函数的提示.该响应应该是JSON.但是我不知道出了什么问题.

我对可能出问题的假设:
1.请求中必须包含一个名为"locations"的参数,如上所述,该参数应为locations:[]类型,那么该如何编写呢?

我是否在代码中使用Source和Destination,它们已经是JSON对象,还是我做错了什么.

如果有人对此有见识,请告诉我.

只是为了好玩,下面的URL显示了我要完成的工作.

在此上花了许多时间,非常感谢您的帮助.

谢谢.



The conditions are that, the request needs to have a parameter named "locations" which should be formatted as locations:[{LocationObjSource},{LocationObjDestination}].

I am also using JSON.stringify() to make request properly formatted as JSON.

All the above code is part of a Razor view in an ASP.NET MVC3 App.

When the request fires, each time, im getting the error function hit. The response is supposed to be JSON. But i don''t know what is going wrong.

My assumptions of what might be wrong:
1. Request needs to contain a PARAMETER named "locations" which should be of type locations:[] as i''ve said above, so how do i compose this?

Do I use Source and Destination as in my code, which are already JSON objects, or am i doing something wrong.

If anyone as an insight to this, please let me know.

Just for fun, the below URL shows what i''m trying to accomplish.

Have spent many hours on this, any help would be very much appreciated.

Thank you.

推荐答案

(''#submit'').click(function(){ var appKey =``MyAppKey''; var source = {street:"Brookings,SD"}; var destination = {街道:苏福尔斯(Sioux Falls,SD)"}; var inputFormat =``json''; var completeRequest = {键:appKey,位置:[源,目标],inFormat:inputFormat};
(''#submit'').click(function () { var appKey = ''MyAppKey''; var source = { street: "Brookings, SD" }; var destination = { street: "Sioux Falls, SD" }; var inputFormat = ''json''; var completeRequest = { key: appKey, locations: [source, destination], inFormat: inputFormat };


.ajax({ 网址:"http://www.mapquestapi.com/directions/v1/route?key=MyAppKey", 输入:"POST", 数据:JSON.stringify(completeRequest), dataType:``json'', processData:否, contentType:"application/json; charset = utf-8", 成功:函数(响应){
.ajax({ url: ''http://www.mapquestapi.com/directions/v1/route?key=MyAppKey'', type: ''POST'', data: JSON.stringify(completeRequest), dataType: ''json'', processData: false, contentType: "application/json; charset=utf-8", success: function (response) {


(``#feedback'').html(response); }, 错误:功能(响应){ alert(发出请求时发生错误" + response.toString()); } }); });
(''#feedback'').html(response); }, error: function (response) { alert("An Error occured when request was made" + response.toString()); } }); });



条件是,请求需要有一个名为"locations"的参数,该参数的格式应为locations:[{LocationObjSource},{LocationObjDestination}].

我还使用JSON.stringify()发出正确格式化为JSON的请求.

以上所有代码都是ASP.NET MVC3应用程序中Razor视图的一部分.

每当请求触发时,我都会得到错误函数的提示.该响应应该是JSON.但是我不知道出了什么问题.

我对可能出问题的假设:
1.请求中必须包含一个名为"locations"的参数,如上所述,该参数应为locations:[]类型,那么该如何编写呢?

我是否在代码中使用Source和Destination,它们已经是JSON对象,还是我做错了什么.

如果有人对此有见识,请告诉我.

只是为了好玩,下面的URL显示了我要完成的工作.

在此上花了许多时间,非常感谢您的帮助.

谢谢.



The conditions are that, the request needs to have a parameter named "locations" which should be formatted as locations:[{LocationObjSource},{LocationObjDestination}].

I am also using JSON.stringify() to make request properly formatted as JSON.

All the above code is part of a Razor view in an ASP.NET MVC3 App.

When the request fires, each time, im getting the error function hit. The response is supposed to be JSON. But i don''t know what is going wrong.

My assumptions of what might be wrong:
1. Request needs to contain a PARAMETER named "locations" which should be of type locations:[] as i''ve said above, so how do i compose this?

Do I use Source and Destination as in my code, which are already JSON objects, or am i doing something wrong.

If anyone as an insight to this, please let me know.

Just for fun, the below URL shows what i''m trying to accomplish.

Have spent many hours on this, any help would be very much appreciated.

Thank you.


这篇关于ASP.NET MVC3 Mapquest jQuery POST请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆