什么是角JS相当于该卷曲POST命令的? [英] What is the Angular JS equivalent of this Curl POST command?

查看:126
本文介绍了什么是角JS相当于该卷曲POST命令的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用编程JS角以下的curl命令。我很新的AngularJS,所以我可能失去了一些东西fundamental.To是具体的,这是一个移动应用程序,即使用离子型架构(使用角JS)

curl命令是这样的:

 卷曲-X POST -D用户名= XXXX和放大器;密码= XXXX和放大器;行动=登录&放大器;查看=控制台http://myserver.com/zm/index.php?skin = XML

此命令行完美。

在AngularJS,我有以下的code:

\r
\r

angular.module('starter.controllers')。控制器('MonitorCtrl',功能($ ionicPlatform,$范围,$ HTTP)\r
{\r
            \r
的console.log(*** MAKING请求);\r
                                                 \r
\r
//$http.defaults.headers.post[\"Content-Type] =应用/的X WWW的形式urlen codeD;\r
                                                 \r
 $ HTTP({\r
    网址:'HTTP://myserver.com:9898 / ZM / index.php的皮肤= XML',\r
    方法:'后',\r
    标题:{内容类型:应用程序/ x-WWW的形式urlen codeD,\r
              接受:* / *,\r
       },\r
    transformRequest:函数(OBJ){\r
       无功海峡= [];\r
       对于(OBJ中的VAR P)\r
       str.push(EN codeURIComponent(P)+=+ EN codeURIComponent(OBJ [P]));\r
       无功富= str.join(与&);\r
       的console.log(****回+富);\r
       返回foo的;\r
    },\r
       数据:{用户名:管理员,\r
              密码:XXXX,\r
              动作:登录,\r
              观点:控制台}\r
       \r
  })\r
                                                 \r
.success(函数()\r
{的console.log(**** YAY);\r
})\r
.error(功能(数据,状态,头,配置)\r
{\r
        的console.log(*** OOPS+状态+H:+数据);\r
});\r
                                                 \r
                                                \r
});

\r

\r
\r

当我运行这个code,我看到一个POST请求会到我的服务器(我用httpry嗅探),并从与所需的负载,服务器返回一个200 OK,但我的成功处理程序不会被调用。错误处理程序被调用0的状态


解决方案

好吧,我终于想通了这一切。

一)阿米尔波波维奇是正确的,如果我处理XML,我需要'transmogrify(在一个神奇的方式转换,非卡尔文和霍布斯的读者)JSON到XML。更新后的code我张贴在我的问题正是这么做的和作品。但是,即使这样做这一切,我的网址后(或获得)的失败后。

二)我面临着是因为我在桌面浏览器(Safari浏览器)上运行这个程序,这是Safari浏览器被踢出来的响应,除非它有接受产地整个织补CORS误差

C)当我部署和测试应用程序在手机上,它的工作就像一个魅力。不要问我为什么。

I am trying to program the following Curl command using Angular JS. I'm very new to AngularJS, so I may be missing something fundamental.To be specific, this is a mobile app, that is using the ionic framework (which uses Angular JS)

The curl command is this:

curl -X POST  -d "username=xxxx&password=xxxx&action=login&view=console"  http://myserver.com/zm/index.php?skin=xml

This works perfectly from command line.

In AngularJS, I have the following code:

angular.module('starter.controllers').controller('MonitorCtrl', function($ionicPlatform, $scope,$http)
{
            
console.log("***MAKING REQUEST");
                                                 

//$http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded";
                                                 
 $http({
    url:'http://myserver.com:9898/zm/index.php?skin=xml',
    method:'post',
    headers: {'Content-Type': 'application/x-www-form-urlencoded',
              'Accept': '*/*',
       },
    transformRequest: function(obj) {
       var str = [];
       for(var p in obj)
       str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
       var foo= str.join("&");
       console.log ("****RETURNING "+foo);
       return foo;
    },
       data: {username:'admin',
              password:'xxxx',
              action:'login',
              view:'console'}
       
  })
                                                 
.success (function()
{console.log("****YAY");
})
.error(function(data, status, headers, config)
{
        console.log("***OOPS "+status + " H: "+data);
});
                                                 
                                                
});

When I run this code, I see a POST request going to my server (I am sniffing using httpry) and a 200OK being returned from the server with the required payload, but my success handler is not being called. The error handler is called with a status of 0

解决方案

Okay, I finally figured it all out

a) Amir popovich is right in that if I am dealing with XML, I need to 'transmogrify' (transform in a magical manner, for non Calvin and Hobbes readers) JSON to XML. The updated code I posted in my question does exactly that and works. But even after doing all that, my URL post (or get) was failing.

b) The entire darn CORS error I was facing was because I was running this app in a desktop browser (Safari) and it was Safari that was kicking out the response unless it had Accept Origin

c) When I deployed and tested the app on my phone, it worked like a charm. Don't ask me why

这篇关于什么是角JS相当于该卷曲POST命令的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆