Webservice在本地工作,但不在服务器上工作

时间:2014-06-12 09:54:11

标签: jquery ajax web-services cors

我的问题是在远程计算机上运行web服务,我认为我已经正确设置了CORS,但我认为我必须出错somwwhere,

这是脚本的开头: -     

    $(document).ready(function () {
        jQuery.support.cors = true;
        CallService();
    });

</script>

所以这会调用callaservice(),它再次在本地运行正常: -

var webServiceURL = 'http://example.com/umbraco/webservices/latestnews.asmx/GetNewsItems';
 function CallService()
{


    $.ajax({
       type: "POST",
       url: webServiceURL,
       beforeSend: function (xhr) {
           xhr.setRequestHeader("SOAPAction", "getNextJobId");
       },
       dataType: "xml",
       crossDomain: true,
       headers: { "X-Requested-With": "XMLHttpRequest" },
       processData: false,
       success: OnSuccess, 
       error: OnError
    });
    return false;
}

function OnSuccess(xml)
{

  $(xml).find('NewsItem').each(
function () {
            var myData = ' <li><a  target="_blank" href="' + $(this).find('path').text() + '">' + $(this).find('Header').text() + '</a> </li>';
//var myData= $(this).find('Header').text() + $(this).find('path').text() ;
    $("#ul1").append(myData);
    }); 
}

function OnError(request, status, error)
{
    alert('Error');
}

我仅限于在服务器上可以执行的操作,但为什么上面的代码在我的本地计算机上工作正常而不是远程?

在网络配置中,我添加了以下内容,我认为允许跨域调用: -

 <system.webServer>
  <httpProtocol>
  <customHeaders>
    <add name="Access-Control-Allow-Origin" value="*" />
  </customHeaders>
    </httpProtocol>
         <handlers>
    <add name="ELMAH" verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" preCondition="integratedMode" />
  </handlers>
</system.webServer>

我得到的错误: -

1- when I browse the the service I cannot test it,         
2- when I put the full URl in i get Request format is unrecognized for URL unexpectedly ending in '/GetNewsItems'.
3- when I  look at the even log :- Exception information: 
    Exception type: InvalidOperationException 
    Exception message: Request format is unrecognized for URL unexpectedly ending in '/GetNewsItems'.
   at System.Web.Services.Protocols.WebServiceHandlerFactory.CoreGetHandler(Type type, HttpContext context, HttpRequest request, HttpResponse response)
   at System.Web.Services.Protocols.WebServiceHandlerFactory.GetHandler(HttpContext context, String verb, String url, String filePath)
   at System.Web.Script.Services.ScriptHandlerFactory.GetHandler(HttpContext context, String requestType, String url, String pathTranslated)
   at System.Web.HttpApplication.MaterializeHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
   at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

0 个答案:

没有答案