【问题标题】:From curl to jQuery AJAX post从 curl 到 jQuery AJAX 帖子
【发布时间】:2014-04-23 10:27:52
【问题描述】:

您好,我想将此 cmd curl 转换为 JQuery Ajax Post:

curl -k -X POST https://openshift.redhat.com/broker/rest/domains/MyDomain/applications 
--user "user@myemail.com:password" 
--data "name=myapp&cartridge=php-5.3&scale=true"

这就是我所做的:

 jQuery.ajax({
     url: 'https://openshift.redhat.com/broker/rest/domains/MyDomain/applications ',
     username: 'user@myemail.com',
     password: 'password',
     type: 'POST',
     dataType: 'json',
     contentType: 'application/json',
     processData: false,
     data: 'name=myapp&cartridge=php-5.3&scale=true"',
     success: function (data) {
         alert(JSON.stringify(data));
     },
     error: function(xhr, status, error) {
        alert("(" + xhr.responseText + ")");
     }
 });

我在我的警报中提交后收到此错误:

(<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
 <title>404 Not Found</title>
</head><body>
 <h1>Not Found</h1>
<p>The requested URL /https://openshift.redhat.com/broker/rest/domains/MyDomain/applications      was not found on this server.</p>
  <hr>
  <address>Apache/2.4.6 (Fedora) Server at drupal-pcsol.openshift.local Port 80</address>
 </body></html>
)

有什么想法吗?

【问题讨论】:

  • in error: function(xhr, status, error) {你能提醒xhr.responseText的值吗?
  • 它为空,xhr.responseText 中没有任何内容
  • 是的,这就是问题所在。 eval("()"); 会给你语法错误。
  • 谢谢,删除 eval 删除了 syntaxe error 但我仍然有 xhr.responseText 为空,任何想法
  • data: 'name=myapp&amp;cartridge=php-5.3&amp;scale=true"', 不是contentType: 'application/json',

标签: javascript php jquery ajax curl


【解决方案1】:

AFAICS,这是一个跨域请求,在 Ajax 中是不允许的。

了解更多:Same-origin policy

【讨论】:

    猜你喜欢
    • 2023-03-11
    • 2010-11-22
    • 2011-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-02
    • 2011-06-01
    相关资源
    最近更新 更多