我創建了一個具有兩個輸入和一個提交按鈕的表單。該表單將發佈到所有已建立的RESTful服務。使用AJAX和返回對象的POST表單數據
我想使用AJAX爲了發佈到這個RESTful服務,然後返回對象,以便我可以驗證表單。
對象將返回類似這樣的一個錯誤,一個成功的
{"status":"error","message":"Incorrect username or password."}
類似我的代碼如下。當我測試我在本地主機上使用XAMPP時:81。當我提交表單時,我收到此錯誤。
No 'Access-Control-Allow-Origin' header is present on the requested resource.
<form name="login-form" id="login-form" method="post" action="SERVICEHERE">
<label id="error">Sorry it seems your credentials are incorrect</label>
<div class="inputBlock">
<label for="username">Username</label>
<input type="text" id="username" name="username"/>
</div>
<div class="inputBlock">
<label for="password">Password</label>
<input type="password" id="password" name="password"/>
</div>
<input type="submit" value="Login" id="submit" />
</form>
$(document).ready(function() {
$("#login-form").submit(function (e) {
e.preventDefault();
var formData = $("#login-form").serialize();
console.log(formData);
$.ajax({
type: "GET",
url: $("#login-form").attr('action'),
data: formData,
success: function(data) {
alert(data);
}
});
});
});
你使用的值SERVICEHERE? – Nicomak
如果有任何幫助,它將返回200和XHR類型的狀態? –
生成此html表單頁面的域名是否相同? – Nicomak