【问题标题】:error in sending a post request using ajax使用 ajax 发送 post 请求时出错
【发布时间】:2016-12-09 11:10:20
【问题描述】:

我正在尝试使用 ajax 发送发布请求,但我不断收到以下错误:

XMLHttpRequest 无法加载 http://192.168.1.123:8080。对预检请求的响应未通过访问控制检查:请求的资源上不存在“Access-Control-Allow-Origin”标头。 Origin 'http://localhost:8080' 因此不允许访问。

这是我的代码

$.ajax({
      type: "POST",
      url: "http://192.168.1.123:8080",
      data: JSON.stringify([{"VisitorName ": " "+document.getElementById("VisitorName ").value}
    ]),

      contentType: "application / json ",
      crossDomain: true,
      dataType: "json",
      success: function (data) {
                alert(data);
      },
      failure: function (errMsg) {
                alert(errMsg);
      }
 });

【问题讨论】:

  • 这是 CORS 错误,服务器不允许除同一域之外的任何其他客户端进行请求,请参阅此以获取更多信息stackoverflow.com/questions/25923796/cors-error-with-jquery
  • 您的代码也有一些语法错误。就像在url 上一样,您不要关闭",data 等代码中抛出的,

标签: javascript jquery ajax post http-post


【解决方案1】:
   $.ajax({
      type: "POST",
      url: "http://192.168.1.123:8080,
                data: JSON.stringify([
                {
                    "
      VisitorName ": "
      "+document.getElementById("
      VisitorName ").value,
      }

据我所知,value 旁边的逗号导致语法错误。同样在代码中,您不会关闭 ""http://192.168.1.123:8080

【讨论】:

  • 抱歉,我一定是复制了旧代码而不是新代码。
  • @nero 好吧,那是有道理的
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-09
  • 1970-01-01
  • 1970-01-01
  • 2019-10-15
  • 2022-09-23
相关资源
最近更新 更多