【问题标题】:sendAJAX data parameter in CasperJsCasperJs 中的 sendAJAX 数据参数
【发布时间】:2015-02-02 05:20:04
【问题描述】:

我再次遇到了 casperjs 的另一个问题,现在使用的是 sendAJAX 函数。

它说 sendAJAX 有 5 个参数,如下所示:

  • url:请求的url。
  • method:HTTP 方法(默认:GET)。
  • data:请求参数(默认:null)。
  • async:异步请求的标志? (默认:假)
  • settings:执行AJAX请求时的其他设置(默认: 空)

所以,它说数据方法是对象所以,它应该填充:

var data = new Object();
data.amount= 15;

还有这个,

var data = {amount:15};

但是没有成功的值发送到我的 web 服务(总是发送 0 作为值,但是 ajax 请求成功,甚至返回 json 数据),它有一个这样的 url

"http://localhost:9000/TempCountryAmountREST/setCountryAmount"

如果我像这样直接将我的数据变量绑定到我的 url 将会成功:

"http://localhost:9000/TempCountryAmountREST/setCountryAmount?amount="+amount

[更新] TempCountryAmountREST 是我的控制器名称,setCountryAmount 是我在控制器中的函数。

[更新] 我忘了包括我对 sendAJAX() 的使用,这是我使用的代码:

return JSON.parse(__utils__.sendAJAX(wsurl, "POST" , data, false, { contentType: "application/json" }));

那么如何填写sendAJAX参数中的数据呢?

提前谢谢...

【问题讨论】:

    标签: ajax casperjs


    【解决方案1】:

    抱歉,我已经找到答案了。

    我在 contentType 中犯了一些错误,我使用 contentType: "application/json" 而不是 contentType: "application/x-www-form-urlencoded" }

    如果我们正在研究 ajax 如何从方法 send() 发送内容,他们使用的是 x-www-form-urlencoded。详情请见this

    当我们看透 casperjs clientutils.js 脚本时,我们应该知道 sendAJAX 是如何工作的。 在 `this.sendAJAX = function sendAJAX(url, method, data, async, settings) { }

    有 url 构造逻辑将我们的 Object(如果是的话)转换为 x-www-form-urlencoded 形式。所以我们需要将我们的contentType设置为application/x-www-form-urlencoded

    很好,感谢您的关注...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多