<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.js" type="text/javascript"></script>
<script>
$.get("http://example.com/", function(data) {
alert(data);
});
</script>
它对那个URL做一个OPTIONS请求,然后回调永远不会被调用任何东西。
当它不是跨域时,它工作得很好。
jQuery不应该只是调用一个<script>节点,然后在它加载时做回调吗?我知道我将无法得到的结果(因为它是跨域),但这是可以的;我只想让电话接通。这是bug,还是我做错了什么?
如果你想POST
确保JSON。Stringify您的表单数据和发送文本/纯。
<form id="my-form" onSubmit="return postMyFormData();">
<input type="text" name="name" placeholder="Your Name" required>
<input type="email" name="email" placeholder="Your Email" required>
<input type="submit" value="Submit My Form">
</form>
function postMyFormData() {
var formData = $('#my-form').serializeArray();
formData = formData.reduce(function(obj, item) {
obj[item.name] = item.value;
return obj;
}, {});
formData = JSON.stringify(formData);
$.ajax({
type: "POST",
url: "https://website.com/path",
data: formData,
success: function() { ... },
dataType: "text",
contentType : "text/plain"
});
}
只需将“application/json”更改为“text/plain”,不要忘记json .stringify(request):
var request = {Company: sapws.dbName, UserName: username, Password: userpass};
console.log(request);
$.ajax({
type: "POST",
url: this.wsUrl + "/Login",
contentType: "text/plain",
data: JSON.stringify(request),
crossDomain: true,
});