【问题标题】:jQuery $.ajax method displays error statusjQuery $.ajax 方法显示错误状态
【发布时间】:2013-05-21 09:18:19
【问题描述】:

网络服务看起来像

using System;<br/>
using System.Collections.Generic;<br/>
using System.Linq;<br/>
using System.Web;<br/>
using System.Web.Services;<br/>
[WebService(Namespace = "http://tempuri.org/")]<br/>
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]<br/>
 [System.Web.Script.Services.ScriptService] <br/>
public class Map : System.Web.Services.WebService
{
   [WebMethod]
    public MapClass LatitudeLongitude()
    {
       MapClass mapobj = new MapClass();
       return mapobj;

    }
     [WebMethod]
  public string helloToYou() 
  { 

      return "Hello"; 
 } 

}


类的样子:

public class MapClass <br/>
{

    string latitude;

    string longitude;

    public MapClass()

    {

        latitude = "51.508742";
        longitude = "-0.120850";
    }

}

我正在尝试从 html5 页面调用网络服务

`</head>`<br/>
`<body>`<br/>

`<form id="form1" runat="server">`<br/>

`<h1>Map</h1>`<br/>

`<input id="View" type="button" value="Click to view" onclick="getData()"/>`<br/>

`</form>`<br/>

`</body>`<br/>


`<script type="text/javascript" src="D:\PepsiCO\jQuery\jquery-1.7.1.min.js"> </script>`<br/>

`<script type="text/javascript">`

    function getData() {

       $.ajax({

          type:"POST",
          url: "http://localhost:53788/HTML5_WebService_Maps/Map.asmx/helloToYou",
          contentType: "application/json;charset=utf-8",
          dataType: "json",
            complete: function (response)
            { alert('complete'); },
            success: function (response)
           { alert('Success'); },
            error: function (response)
            { alert('Failure'); }
        });
    }
`</script>`
`</html>`

当我在 chrome 中浏览 html 页面时,它会在警告框中显示失败。不知道为什么它没有显示成功。请帮忙。

【问题讨论】:

  • 尝试将data: '{}', 添加到您的ajax 函数中。不确定它是否会起作用。
  • 确保您的服务器返回内容类型application/json
  • 还将 url 设置为 url: "Map.asmx/helloToYou",
  • @munnebShabbir 如何检查服务器是否返回内容类型'application/json'
  • 错误信息是什么?

标签: jquery asp.net json html web-services


【解决方案1】:

将dataType更改为html并检查

dataType: "html",

【讨论】:

    【解决方案2】:

    您的网页是否在同一个域/端口上运行?响应的状态码是什么?

    【讨论】:

    • 域名/端口呢? chrome 控制台中的任何条目?
    • 我创建的网页是一个简单的 html5 页面,我从 Visual Studio 2010 浏览。我没有在 IIS 中托管服务。
    • 我尝试在 IE 中加载 html 页面。状态文本为无响应。状态为 0。响应文本未定义
    • 状态文本为无传输
    【解决方案3】:

    尝试将此附加到网络方法:

    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    

    并使用序列化器返回对象:

    JavaScriptSerializer js = new JavaScriptSerializer();
    string strJSON = js.Serialize(mapobj);
    return strJSON;
    

    发布错误响应也是一个好主意:

    error: ajaxFailed
    
    function ajaxFailed(xmlRequest) {
    $('#errordiv).html(xmlRequest.status + ' <br /> ' +
                xmlRequest.statusText + '<br />' +
                xmlRequest.responseText);
    }
    

    针对您对此答案的评论: 引用 https://stackoverflow.com/users/158502/langdon '根据我的经验,在执行跨站点脚本(访问被拒绝)或请求无法访问的 URL(错字、DNS 问题等)时,您会看到状态为 0。'

    jQuery Ajax - Status Code 0?

    【讨论】:

    • 您不必序列化必须返回的数据。 asp.net 会自动为您完成。
    • 状态为 0; statusText 是错误;响应文本是空白
    • 根据我的经验,在执行跨站点脚本(访问被拒绝)或请求无法访问的 URL(错字、DNS 问题等)时,您会看到状态为 0。取自:stackoverflow.com/questions/2000609/jquery-ajax-status-code-0
    • 如果您在 IIS 上的 Web 服务托管在与 html5 页面不同的应用程序(因此端口不同)中,则您正在发出跨域请求,这会出现链接问题中描述的问题。如果您将 html5 页面部署到同一个应用程序会怎样?
    • 那么这就是你的问题。看到这个可能的答案:stackoverflow.com/a/10618640/2215083
    猜你喜欢
    • 2014-11-18
    • 1970-01-01
    • 1970-01-01
    • 2018-12-21
    • 1970-01-01
    • 1970-01-01
    • 2012-09-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多