如何更改网址

时间:2015-12-13 07:46:37

标签: javascript ajax

我想更改更新操作的URL。 URL的结束点应该从输入中获取。我怎样才能做到这一点?以下不起作用。

    $(document).ready(function(){
      
       $.fn.serializeObject = function()
{
    var o = {};
    var a = this.serializeArray();
    $.each(a, function() {
        if (o[this.name] !== undefined) {
            if (!o[this.name].push) {
                o[this.name] = [o[this.name]];
            }
            o[this.name].push(this.value || '');
        } else {
            o[this.name] = this.value || '';
        }
    });
    return {"issue":o};
};
<form role="form">
    <div class="form-group input-group">
        <div class="row">
        <div class="col-md-6">
        <label>Issue ID * &nbsp;</label>
        <select class="form-control selectclass" id="issueid"></select>
        </div>
        <div class="col-md-6">
        <label>Tracker * &nbsp;</label>
        <select class="form-control" name="tracker" id="tracker">
          <option value="1">Bug</option>
          <option value="2">Feature</option>
          <option value="3">Support</option>
        </select>
        </div>
        </div>
    </div>
    <div class="form-group">
        <label>Subject &nbsp;</label>
        <input class="form-control" placeholder="Issue Subject" name="subject" id="subject">
    </div>
    <div class="form-group">
        <label>Description</label>
        <textarea class="form-control" rows="6" name="description" id="description"></textarea>
    </div>
  <button type="submit" id="submit" class="btn btn-default">Submit Button</button>
  <button type="reset" class="btn btn-default">Reset Button</button>
  </form>

$('#submit').on('click', function(){
            var x=document.getElementById('issueid').value;
            $.ajax({
                type : 'PUT',
                
                url: 'http://localhost/redmine/issues'+ x +'.json',
               
                
                contentType:'application/json',
                 
                data:JSON.stringify($('form').serializeObject()), // post data || get data
                success : function(msg, status, jqXHR) {
                    console.log(msg, status, jqXHR);
                    
                },
                error: function(xhr, resp, text) {
                    console.log(xhr, resp, text);
                }
            })
            console.log(x);
         });
   
});

输入来自表单并发送到Redmine API。网址应如下所示http://localhost/redmine/issues/2.json

1 个答案:

答案 0 :(得分:1)

动态填充id="issueid"选项时,请确保正确设置选项的value属性:

$('.selectclass').append('<option value="' + value.id + '">' + value.id + '<option>');

还可以在/之后添加尾随issues来修复下一个AJAX请求的网址:

url: 'http://localhost/redmine/issues/' + x + '.json'