简体   繁体   English

使用XMLHttpRequest导轨将有效载荷数据发布到数据库

[英]POST payload data to database using XMLHttpRequest rails

I'm working on a tournament bracket based on the GOjs library , The bracket has score input. 我正在使用基于GOjs库的锦标赛括号,该括号具有得分输入。

Once my user is done editing the bracket I save the bracket into a JSON variable : 一旦用户完成了括号的编辑,我便将括号保存到JSON变量中:

function save() {
    var tojs = myDiagram.model.toJSON();
    var payload = JSON.parse(tojs);
    stringify_tojs = JSON.stringify(payload);
    myDiagram.isModified = false;

I use XMLHttpRequest to able to post the payload into my rails model that handles 'payload' : 我使用XMLHttpRequest来将有效负载发布到处理“有效负载”的rails模型中:

var request = new XMLHttpRequest();
    request.onload = callback;
    request.open("post", "http://localhost:3000/malesingles");
    request.setRequestHeader("Content-Type", "application/json");
    request.send(payload);

I don't know where I went wrong but I'm certain it's around my controller params but I can't find my mistake already been a week, the controller looks something like this : 我不知道我哪里出了错,但是我确定这是在我的控制器参数周围,但是我已经一周都找不到错误了,控制器看起来像这样:

@tournoi = Tournoi.new(bracket_params)
  if @tournoi.save
    redirect_to root_url
    flash[:success] = "Your tournament bracket has been validated!"
  #  redirect_to @tournoi
  else
    render 'new'
  end

end 结束

I have included the bracket_params in private settings 我已经在专用设置中包含了bracket_params

def bracket_params
      params.require(:tournoi).permit(:payload)
     end

Tried different method to post the payload none really work, would appreciate some help to understand where I went wrong, I get a param is missing or empty :/. 尝试了不同的方法来发布有效载荷,但这些方法都无法正常工作,不胜感激,这对理解我出了错的地方有所帮助,我发现参数丢失或为空:/。

@DezzH check out my latest commits from my repo https://github.com/fabriziobertoglio1987/sprachspiel/tree/feature/purchase-system I just built something like that using coffescript.. the commits include description of my work @DezzH从我的仓库中查看我的最新提交https://github.com/fabriziobertoglio1987/sprachspiel/tree/feature/purchase-system我刚刚使用coffescript构建了类似的内容。提交包括对我工作的描述

basically what I figured out is, post request do not pass the parameters in the url so it is not easy to find them in the request, but you can check in your network tab 基本上我发现的是, post请求不会在url中传递参数,因此在请求中查找它们并不容易,但是您可以在“网络”标签中检查

as you can see from the image I am passing 从图像中可以看到

 createPurchase: -> 
    $.ajax
       url: "/products"
       method: "POST"
       dataType: "json"
       data: { items: {product_id: '1', name: 'test' }}
       error: (jqXHR, textStatus, errorThrown) ->
         console.log "AJAX Error: #{textStatus}"
       success: (data, textStatus, jqXHR) ->
         console.log "Successful AJAX call: #{data}"
         console.log data

在此处输入图片说明

then I set just a binding.pry in the controller and I can see my params there 然后我在控制器中只设置一个binding.pry,我可以在那里看到我的参数

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM