带有编码“multipart / form-data”的AJAX POST表单成功但返回错误

时间:2015-08-11 06:14:31

标签: jquery html ajax post

我使用HTML表单使用AJAX将包含文件的一些数据提交到HTTP可访问的Java WebService。它具有文件输入,单选按钮,复选框和选择选项。

<form enctype="multipart/form-data" action="" method="post" id="validate-form">
    <div id="subscribers_csv" class="fileinput fileinput-new" data-provides="fileinput">
        <span class="btn btn-default btn-file">
            <span class="fileinput-new">Select file</span>
            <span class="fileinput-exists">Change</span>
            <input type="file" id="subscribersCSV" name="subscribersCSV" />
        </span>
        <span class="fileinput-filename"></span>
        <a href="#" class="close fileinput-exists" data-dismiss="fileinput" style="float: none">&times;</a>
    </div>

    <label class="radio-inline"><input type="radio" name="service" id="voicemail" checked value="voicemail" /> Voicemail</label>
    <label class="radio-inline"><input type="radio" name="service" id="voicesms" value="voicesms" /> VoiceSMS</label>

    <div class="toggle" onclick="manageMailboxGroup();"></div>
    <input type="checkbox" value="" name="check_mailbox_group" id="check_mailbox_group" style="display:none">

    <select class="form-control" name="group_name" id="group_name">
          <option value="1">1</option>
          <option value="2">2</option>
    </select>

然后我通过按钮上的单击事件并使用AJAX调用Java WebService来提交它。

var url= "http://IP:port/ServiceName";
$.ajax({
    url:url,
    data: formData,
    type:"POST",
    cache: false,
    processData: false,
    contentType: false,
    success: function(data){
        alert(data);
    },
    error: function(error) {
        alert("AJAX Error");
    }
});

当我提交表单时,会正确调用WebService,然后处理数据而不会出现任何问题。但是当它返回时,它总是转到“错误”回调并警告“AJAX错误”。

我应该在表单或AJAX调用中使用其他东西吗?我用Google搜索了许多使用多部分表单的例子,它们都与我在这里的相似。感谢。

ETA: 我应该补充说,Java WebService和HTML / JQuery代码位于同一网络上的不同服务器上。我在控制台中得到了这个:“请求资源上没有'Access-Control-Allow-Origin'标题。因此不允许原点'http://IP:Port'访问。”

0 个答案:

没有答案