【问题标题】:Using jquery to pull from ajax response使用 jquery 从 ajax 响应中提取
【发布时间】:2012-06-15 15:32:59
【问题描述】:
http://localhost:2000/WebService/Handler.ashx?d=ck

查看 URL 时的响应:

{"Status":"OK","Message":"0","Info":"(none)"}

使用 JQuery 我如何从中提取?我知道你是如何进行 POST 和发送的,但对于如何从中拉出来的东西很少丢失。我使用 GET 吗?

我在做类似 $.get("URL HERE"...?

【问题讨论】:

  • 除非您的 Web 服务返回 CORS 标头或正确的 JSONP,否则如果 localhost:2000 与请求数据的页面的域和端口不同,它将无法工作。

标签: javascript jquery asp.net ajax vb.net


【解决方案1】:
$.ajax({
    url:"...",
    dateType: "json", // <=== you expect "JSON" string
    success: function(data){ 
        alert(data.Status); // Extract the data from the response.
        alert(data.Message);
        alert(data.Info);            
    }        
});

或者简写getJSON函数:

$.getJSON(url,function(data){
    data.Status;
    ...
});

【讨论】:

    【解决方案2】:

    您可以使用ajax 函数来检索您的 JSON 响应:

    $.ajax({
      url: 'http://localhost:2000/WebService/Handler.ashx?d=ck',
      dataType: 'json',
      success: function(data) { 
          console.log(data); 
          console.log('Status: ' + data.Status); 
      }
    });
    

    【讨论】:

      【解决方案3】:

      使用jquery的jQuery.ajaxjQuery.getJSON函数。

       $.ajax({
        url: url,
        dataType: 'json',
        data: data,
        success: callback
      });
      

      或者这样……

       $.getJSON('ajax/test.json', {data: data },function(data) {
            // do as needed
       });
      

      【讨论】:

        猜你喜欢
        • 2017-08-30
        • 1970-01-01
        • 2013-01-20
        • 1970-01-01
        • 2014-10-17
        • 1970-01-01
        • 2011-12-30
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多