如何在c#中转换jquery ajax调用

时间:2016-02-27 06:14:50

标签: c# jquery ajax

我想将此jquery ajax调用转换为c#代码。我可以知道如何转换吗?

$.ajax({
	url:		'http://gothere.sg/maps/geo',
	dataType:	'jsonp',
	data:		{
		'output'	: 'json',
		'q'		: address,
		'client'	: '',
		'sensor'	: false
	},
	type:	'GET',
	success: function(data) {
    				
	},
	statusCode: {
		404: function() {
			alert('Page not found');
		}
	},
});

我的C#代码返回404.如何在C#中指定dataType,data和type。 我可以知道如何在C#中传递这三个参数。

class Program
{
    static void Main()
    {
        RunAsync().Wait();
    }

    static async Task RunAsync()
    {

            // TODO - Send HTTP requests
            var url = "http://gothere.sg/maps/geo";
            var dataType = "jsonp";
            var type = "GET";

            Console.WriteLine("Making API Call...");
            using (var client = new HttpClient(new HttpClientHandler { AutomaticDecompression = System.Net.DecompressionMethods.GZip | System.Net.DecompressionMethods.Deflate }))
            {
                client.BaseAddress = new Uri(url);

                HttpResponseMessage response = client.GetAsync("data?output=json&q=550238&client=''&sensor=false").Result;
                response.EnsureSuccessStatusCode();
                string result = response.Content.ReadAsStringAsync().Result;
                Console.WriteLine("Result: " + result);
            }
            Console.ReadLine();

    }
}

2 个答案:

答案 0 :(得分:0)

您可能只需要在网址上添加一个斜杠。否则,请求将转到http://gothere.sg/maps/geodata?output=...,您将获得404。

所以将其改为

var url = "http://gothere.sg/maps/geo/";

答案 1 :(得分:0)

尝试将callback=参数添加到网址。基本上你要求jsonp资源。

所以url应该有回调参数

http://gothere.sg/maps/geo?callback=&output=json&q=dell&client=&sensor=false

导航到此网址,您可以看到结果,如果删除回调参数,则无法使用。