【问题标题】:Posting json with ajax issue使用ajax问题发布json
【发布时间】:2016-07-02 10:42:53
【问题描述】:

我有以下代码,总是产生 404 错误(未找到):

data = Object {a: "500000", b: "4"}
postJson(data);


function postJson(data){
    $.ajax({
                url: '/url/postJson',
                type: 'POST',
                data: data, //also tried "JSON.stringify(data)"
                dataType: "json",
                contentType: "application/json",
                success: function (data, textStatus, jqXHR) {

                },
                error: function (jqXHR, textStatus, errorThrown) {
                    alert('error')
                }
            });
}

在服务器端:

@cherrypy.expose
def postJson(self, data):
    print data //just for the test

可能是什么问题?

【问题讨论】:

  • 网址是否正确?顺着函数名,不应该是/url/postJson吗?
  • @BrianRay 我修好了。这是一个错字。这些不是真实姓名。无论如何,真实的网址是正确的。这是我检查的第一件事。
  • 嗯,它与 URL 有关。这是获得 404 的唯一方法。但是,在不知道目录结构的情况下,我无能为力。
  • 当我尝试直接浏览此 URL 时,我得到 HTTPError: (404, 'Missing parameters: data') 所以我猜 URL 不是问题。
  • 浏览器使用 GET 方法,而不是 POST。

标签: jquery json ajax cherrypy


【解决方案1】:

关于服务器变化:

@cherrypy.expose
def postJson(self, data):
    print data //just for the test

到:

@cherrypy.expose
@cherrypy.tools.json_in
def postJson(self):
    data = cherrypy.request.json
    print data //just for the test

【讨论】:

    【解决方案2】:

    根据this answer 中的代码,您的 JSON 格式似乎需要更新。试试这个。

    data = {data: {a: "500000", b: "4"}}
    

    【讨论】:

      【解决方案3】:

      404 是来自服务器端的状态。 要检查是否存在并访问您的 url,请尝试 curl:

      curl -X POST -H "Content-Type: application/json" --data '{a: "500000", b: "4"}' http://url/portJson
      

      我经常使用 100% 的 POST Django 后端工作功能(我没有设置 'contentType' 和 'dataType',但我设置了 'X-CSRFToken'):

      function sendAjaxRequest(url, data, done, always, fail, reqType, timeout) {
          var _done = function() {};
          var _always = function() {};
          var _fail = function(jqXHR,status,err){
              alert('Error (' + status + '): ' + err + '\n' + jqXHR.responseText);
          }
          var _reqType ='POST'
          var _timeout = 60000;
      
          if (typeof done !== 'undefined') _done = done;
          if (typeof always !== 'undefined') _always = always;
          if (typeof fail !== 'undefined') _fail = fail;
          if (typeof reqType !== 'undefined') _reqType = reqType;
          if (typeof timeout !== 'undefined') _timeout = timeout;
      
          var csrftoken = $.cookie('csrftoken');
      
          $.ajax({
              url: url,
              headers:{ 'X-CSRFToken': csrftoken },
              data: data,
              cache: false,
              type: _reqType,
              timeout: _timeout
          }).done(_done).always(_always).fail(_fail);
      };
      

      使用:

      var data = {'a': "5000", 'b': "4"};
      
      sendAjaxRequest(
          "/my/url/",
          data,
          function(response, textStatus, jqXHR) {// done
             // do work
          }, undefined, undefined, 'POST'
      );
      

      【讨论】:

        猜你喜欢
        • 2018-12-12
        • 1970-01-01
        • 2013-01-10
        • 2015-11-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多