【问题标题】:jQuery POST redirects to http://127.0.0.1:8080/MyApp/[object Object]jQuery POST 重定向到 http://127.0.0.1:8080/MyApp/[object Object]
【发布时间】:2016-11-29 03:05:48
【问题描述】:

在我的 jQuery POST 请求中,url 总是被定向到本地主机(带有消息 http://127.0.0.1:8080/MyApp/[object Object]

使用 jQuery GET 可以正常工作(连接到外部 URL)。

如何更改 POST 行为以访问指定的 URL?

var tmpurl='https://myhost.com/message';
$.post({
  data: {
   'v': value,
   'key' : 'MyKey'
  },
  dataType: 'jsonp',
  url: tmpurl,
  success: function(response) {
  console.log("success!", response);  
 }
}); 

【问题讨论】:

  • 重定向是什么意思?并尝试使用 json 作为 dataType 而不是 jsonp
  • 我的意思是它不使用预期的 url (myhost.com/message) 作为目标,而是尝试连接到 localhost。 jsonp 用于跨域请求。尝试使用 json dataType,但不起作用。

标签: jquery url post localhost


【解决方案1】:

由于您是通过 https 进行 ajax 调用,我建议您在末尾添加一个额外的 /,例如 /message/。对于跨域请求,请使用crossDomain: true.

var tmpurl='https://myhost.com/message/';
$.post({
  data: {
   'v': value,
   'key' : 'MyKey'
  },
  crossDomain: true,
  dataType: 'json',
  url: tmpurl,
  success: function(response) {
  console.log("success!", response);  
 }
}); 

这应该可以解决您的问题。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-07-20
  • 1970-01-01
  • 2023-02-17
  • 2017-10-08
  • 1970-01-01
  • 1970-01-01
  • 2018-01-27
相关资源
最近更新 更多