【问题标题】:Is it possible to submit a standard HTML form (using jQuery/Ajax) to another standard HTML form on another website?是否可以将标准 HTML 表单(使用 jQuery/Ajax)提交到另一个网站上的另一个标准 HTML 表单?
【发布时间】:2014-04-30 16:26:43
【问题描述】:

如果能回答这个问题,我将不胜感激。这真的可能吗?

假设 website1.com 有一个非常简单的表单:

<form id="form1" name="form1" method="post">
  <input type="text" name="textfield2" id="textfield2">
  <input type="text" name="textfield" id="textfield">
  <input type="submit" name="submit" id="submit" value="Submit">
</form>

然后我在 website2.com 上复制这个:

<form id="form1" name="form1" method="post">
  <input type="text" name="textfield2" id="textfield2">
  <input type="text" name="textfield" id="textfield">
  <input type="submit" name="submit" id="submit" value="Submit">
</form>

如果您将 website2.com 上的表单操作更改为指向 website1.com,您可以轻松提交表单数据。这真的很容易。但是,如果您仍想提交数据,但在提交表单时不想被带到 website1.com,您可以使用哪些解决方案?例如,您可能希望显示自定义成功页面。

我只能想到使用 jQuery/Ajax,但是你会遇到跨域安全问题,如果你尝试使用 jsonp 来规避这个问题,你最终会得到诸如“Uncaught SyntaxError: Unexpected token

你到底会怎么做呢?我变得有点卡住了。还是只是一种情况,除非使用 PHP 等服务器端脚本语言,否则这实际上是不可能的?

【问题讨论】:

  • 您可以在 website2.com 上使用 AJAX 发布到 website2.com 上的 PHP 文件,该文件使用 CURL 或其他东西发布到 website1.com。除非您打开 website1.com 以允许 AJAX 访问。
  • 如果您使用 PHP,您可以使用 cURL 函数远程发布。 AJAX 东西会处理您的“本地”帖子,但您将无法恢复状态。 php.net/manual/en/book.curl.php
  • 我觉得如果我回答这个问题我可以被认为是同谋......
  • 可以通过在服务器上设置allow-crossdomain-header来解决crossdomain-problem
  • @JohnnyAW 我什至没有意识到这是可能的。我会调查的...

标签: jquery ajax forms


【解决方案1】:

我在 Apache 服务器上的 htaccess 文件中添加了以下内容:

Header set Access-Control-Allow-Origin "*"

参考:http://enable-cors.org/server_apache.html

然后我可以只进行标准的 Ajax 调用,而不必担心 jsonp。

感谢@JohnnyAW 的建议。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-08-12
    • 1970-01-01
    • 2023-03-23
    • 1970-01-01
    • 2015-12-12
    • 2013-08-18
    • 2016-12-05
    相关资源
    最近更新 更多