草庐IT

php - $http - Angular post 参数并从 PHP 获取 json

coder 2024-04-21 原文

我是 Angular 的新手,使用 $http 甚至是更新的。我无法得到以下内容_

  • 使用 $http 发布参数(PHP 执行调用所需的参数)
  • 获取 JSON 作为对该调用的响应

这是我到目前为止得到的:

$http 调用:

var deferred = $q.defer();
var parametres = $.param({ nomWS: ws, servidor: ip, query: param, idCamping: dadesCamping[0], forceRenew: renew});       

var url = './api/functions.php?function=callWS_JSON'; 

$http({
    method: 'POST',
    url: url,
    data: parametres,
    headers: {'Content-Type': 'application/x-www-form-urlencoded'}
}).success(function(data) {            
    console.log('success');
    deferred.resolve(data);
}); 

接收调用的 php 是使用通过 $_POST[] 发送的参数检索内容并使用以下方法获取 json:

$fileContents = file_get_contents($url);        
echo $fileContents;

如果我在 PHP 脚本中手动设置参数,它会输出一个有效的 JSON 字符串,但是当我回显它以便在我的 javascript( Angular )代码中检索它时,我在 Chrome 控制台中收到以下错误:

SyntaxError: Unexpected token &
at Object.parse (native)
at Vb (http://domain/lib/angular/angular.js:14:208)
at e.defaults.transformResponse (http://domain/lib/angular/angular.js:64:454)
at http://domain/lib/angular/angular.js:64:215
at Array.forEach (native)
at r (http://domain/lib/angular/angular.js:7:280)
at pc (http://domain/lib/angular/angular.js:64:197)
at c (http://domain/lib/angular/angular.js:65:400)
at C (http://domain/lib/angular/angular.js:94:187)
at http://domain/lib/angular/angular.js:95:350

如果我在回显内容之前回显某些内容,我不会收到错误并且控制台输出以下内容:

test
{"clientesPresentes": {"cliente":...

(剪掉了输出,但是思路很清晰)

我假设 SyntaxError: Unexpected token & 是因为输出。我很确定问题出在标题和内容类型上。试图找到执行后调用的参数并获取 json 内容作为答案,但我没有设法找到答案。因此,我们将不胜感激任何帮助。

提前致谢

PD: 试图在我的 php 文件中设置 header('Content-type: application/json'); 但我得到相同的 SyntaxError: Unexpected token & 错误

提前致谢!

最佳答案

参数在哪里?

我是这样用的。

var $scope.someData = {
   'param1': 12,
   'param2': 33
};

//someData var can also be set inside a form

<input type="text" ng-model="somedata.param1" >

//post and get available
$http.post('url.php' , $scope.someData)
    .success(function(res){
      ;
             })
    .error(function(error){
      ;
    });

并在 php 文件中以这种方式获取变量

$data = file_get_contents("php://input");
echo $data->param1;

关于php - $http - Angular post 参数并从 PHP 获取 json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24145107/

有关php - $http - Angular post 参数并从 PHP 获取 json的更多相关文章

  1. ruby-on-rails - 如何在 ruby​​ 中使用两个参数异步运行 exe? - 2

    exe应该在我打开页面时运行。异步进程需要运行。有什么方法可以在ruby​​中使用两个参数异步运行exe吗?我已经尝试过ruby​​命令-system()、exec()但它正在等待过程完成。我需要用参数启动exe,无需等待进程完成是否有任何ruby​​gems会支持我的问题? 最佳答案 您可以使用Process.spawn和Process.wait2:pid=Process.spawn'your.exe','--option'#Later...pid,status=Process.wait2pid您的程序将作为解释器的子进程执行。除

  2. ruby - RSpec - 使用测试替身作为 block 参数 - 2

    我有一些Ruby代码,如下所示:Something.createdo|x|x.foo=barend我想编写一个测试,它使用double代替block参数x,这样我就可以调用:x_double.should_receive(:foo).with("whatever").这可能吗? 最佳答案 specify'something'dox=doublex.should_receive(:foo=).with("whatever")Something.should_receive(:create).and_yield(x)#callthere

  3. ruby - 如何模拟 Net::HTTP::Post? - 2

    是的,我知道最好使用webmock,但我想知道如何在RSpec中模拟此方法:defmethod_to_testurl=URI.parseurireq=Net::HTTP::Post.newurl.pathres=Net::HTTP.start(url.host,url.port)do|http|http.requestreq,foo:1endresend这是RSpec:let(:uri){'http://example.com'}specify'HTTPcall'dohttp=mock:httpNet::HTTP.stub!(:start).and_yieldhttphttp.shou

  4. ruby-on-rails - Rails HTML 请求渲染 JSON - 2

    在我的Controller中,我通过以下方式在我的index方法中支持HTML和JSON:respond_todo|format|format.htmlformat.json{renderjson:@user}end在浏览器中拉起它时,它会自然地以HTML呈现。但是,当我对/user资源进行内容类型为application/json的curl调用时(因为它是索引方法),我仍然将HTML作为响应。如何获取JSON作为响应?我还需要说明什么? 最佳答案 您应该将.json附加到请求的url,提供的格式在routes.rb的路径中定义。这

  5. ruby - 如何在 Ruby 中拆分参数字符串 Bash 样式? - 2

    我正在为一个项目制作一个简单的shell,我希望像在Bash中一样解析参数字符串。foobar"helloworld"fooz应该变成:["foo","bar","helloworld","fooz"]等等。到目前为止,我一直在使用CSV::parse_line,将列分隔符设置为""和.compact输出。问题是我现在必须选择是要支持单引号还是双引号。CSV不支持超过一个分隔符。Python有一个名为shlex的模块:>>>shlex.split("Test'helloworld'foo")['Test','helloworld','foo']>>>shlex.split('Test"

  6. ruby - 检查方法参数的类型 - 2

    我不确定传递给方法的对象的类型是否正确。我可能会将一个字符串传递给一个只能处理整数的函数。某种运行时保证怎么样?我看不到比以下更好的选择:defsomeFixNumMangler(input)raise"wrongtype:integerrequired"unlessinput.class==FixNumother_stuffend有更好的选择吗? 最佳答案 使用Kernel#Integer在使用之前转换输入的方法。当无法以任何合理的方式将输入转换为整数时,它将引发ArgumentError。defmy_method(number)

  7. ruby-on-rails - 在默认方法参数中使用 .reverse_merge 或 .merge - 2

    两者都可以defsetup(options={})options.reverse_merge:size=>25,:velocity=>10end和defsetup(options={}){:size=>25,:velocity=>10}.merge(options)end在方法的参数中分配默认值。问题是:哪个更好?您更愿意使用哪一个?在性能、代码可读性或其他方面有什么不同吗?编辑:我无意中添加了bang(!)...并不是要询问nobang方法与bang方法之间的区别 最佳答案 我倾向于使用reverse_merge方法:option

  8. ruby - 简单获取法拉第超时 - 2

    有没有办法在这个简单的get方法中添加超时选项?我正在使用法拉第3.3。Faraday.get(url)四处寻找,我只能先发起连接后应用超时选项,然后应用超时选项。或者有什么简单的方法?这就是我现在正在做的:conn=Faraday.newresponse=conn.getdo|req|req.urlurlreq.options.timeout=2#2secondsend 最佳答案 试试这个:conn=Faraday.newdo|conn|conn.options.timeout=20endresponse=conn.get(url

  9. ruby - 定义方法参数的条件 - 2

    我有一个只接受一个参数的方法:defmy_method(number)end如果使用number调用方法,我该如何引发错误??通常,我如何定义方法参数的条件?比如我想在调用的时候报错:my_method(1) 最佳答案 您可以添加guard在函数的开头,如果参数无效则引发异常。例如:defmy_method(number)failArgumentError,"Inputshouldbegreaterthanorequalto2"ifnumbereputse.messageend#=>Inputshouldbegreaterthano

  10. ruby - 从 Ruby 中的主机名获取 IP 地址 - 2

    我有一个存储主机名的Ruby数组server_names。如果我打印出来,它看起来像这样:["hostname.abc.com","hostname2.abc.com","hostname3.abc.com"]相当标准。我想要做的是获取这些服务器的IP(可能将它们存储在另一个变量中)。看起来IPSocket类可以做到这一点,但我不确定如何使用IPSocket类遍历它。如果它只是尝试像这样打印出IP:server_names.eachdo|name|IPSocket::getaddress(name)pnameend它提示我没有提供服务器名称。这是语法问题还是我没有正确使用类?输出:ge

随机推荐