Jquery post中的网络错误
我在执行Jquery post时出错(错误:发生网络错误) 对于邮递员来说,它确实有效,下面是卷曲:Jquery post中的网络错误,jquery,post,Jquery,Post,我在执行Jquery post时出错(错误:发生网络错误) 对于邮递员来说,它确实有效,下面是卷曲: POST /site/restapi/postalcode HTTP/1.1 Host: localhost:8080 Content-Type: application/json Cache-Control: no-cache Postman-Token: 14d39deb-fc28-3171-d688-18deaca7cbbe { "postalCode" : "9041AK", "hou
POST /site/restapi/postalcode HTTP/1.1
Host: localhost:8080
Content-Type: application/json
Cache-Control: no-cache
Postman-Token: 14d39deb-fc28-3171-d688-18deaca7cbbe
{
"postalCode" : "9041AK",
"houseNumber" : "1"
}
不幸的是,我的javascript没有:
$('input[name="houseNumber"]').blur(function() {
// Check if the street and city can be fetched
$.ajax({
type : "POST",
contentType : "application/json",
url : "localhost:8080/site/restapi/postalcode",
data: { postalCode : "9713GC", houseNumber : "1" },
dataType: "json",
crossDomain: true,
success: function(data) {
console.log(data);
}}).fail(function(data, status, error) {
console.log("error!" + error);
});
});
编辑:我已经安装了firefox插件篡改数据,这使我能够洞察所有发送的请求。这提供了一些额外的信息:根本没有发送任何请求。(除了加载页面时的GET请求之外。)
发生了什么事?为什么不起作用?请求中的
url
是错误的
如果编写“localhost:8080/site/restapi/postalcode”
,则localhost
将根据url解析器的实现,引用协议或路径将被解释为相对路径,但在这两种情况下localhost:8080
不会被解释为host
您必须编写以下内容之一:
”http://localhost:8080/site/restapi/postalcode“
”https://localhost:8080/site/restapi/postalcode“
“//localhost:8080/site/restapi/postalcode”
“/site/restapi/postalcode”
哪一个取决于您发起请求的url。您是不是碰巧将http与https混用了?这也是跨域呼叫吗?不,不是跨域呼叫。在我所有其他选项都失败后,我添加了这个选项:-P。我怎么能像在本地主机上那样混淆http和https呢?嘿!你知道这是怎么回事,为了以防万一,我们甚至不得不问最愚蠢的问题!:-)。对于postman,您使用的是内容类型,但在AJAX调用中没有设置该标题,您是否尝试过设置该标题?(默认值为application/x-www-form-urlencoded;charset=UTF-8)我添加了以下内容:contentType:“application/json”(我也将在问题中更新此内容。不幸的是,结果是相同的(网络错误)。