使用jquery访问Web服务时出现内部错误500

时间:2011-08-02 12:35:22

标签: jquery web-services

我正在使用jquery从Web服务获取响应。 代码与我见过的许多其他代码一样没有问题。 我正在使用http://www.service-repository.com/中的免费网络服务进行测试。

我的代码如下所示:

jQuery.support.cors = true;

        $.ajax({
            type: "POST",
            url: "http://www.webservicex.com/globalweather.asmx/GetWeather",
            data: "{CityName: Boston}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            error: writeError(){...};
            success: beHappy(){...} });

正如标题所说,我收到内部服务器错误。

  

第一行启用跨域脚本编写。   这是必需的,因为Web服务是远程的。 (没有这一行我得到“No Transport”错误)

1 个答案:

答案 0 :(得分:0)

当我查看网址http://www.webservicex.com/globalweather.asmx/GetWeather?CityName=Boston时,我收到内部服务器错误消息,指出CountryName缺失。 此调用是否需要CountryName?

我通过将您的查询更改为:

来获取此链接
$.ajax({
   type: "GET",
   url: "http://www.webservicex.com/globalweather.asmx/GetWeather",
   data: {'CityName': 'Boston'},
   contentType: "application/json; charset=utf-8",
   dataType: "json",
   success: function(data) {
   alert('done');
   }
});

并在萤火虫中查看GET。当我在www.webservicex.com上查看该服务时,我能得到的是“未找到数据”。你确定这个服务返回JSON,因为当我尝试时,我能得到的就是XML:

$.getJSON('http://www.webservicex.com/globalweather.asmx/GetWeather?callback=?',
{
    'CityName': 'Dublin', 
    'CountryName': 'Ireland'
},
function(data) {
     alert(data);
});

如果你无法通过ajax调用恢复XML。

另一种方法是在服务器端使用Web服务,然后使用ajax调用服务器来获取要添加到页面的html。

相关问题