标签: http-post

如何将JSON数据发送到服务器

好吧,这是故事:

我有一些数据需要发送到服务器,但他们应该首先变成JSON dataType.

我做了这样的ajax电话:

    $.ajax({
       url: url, // the url I want to post to.
       type: 'POST',
       contenttype:'application/json; charset=utf-8',
       beforeSend: //some HTTP basic auth stuff
       data: {
          name:'test',
          key:'foo',
          key2:'bar'
       },
       dataType:'JSON'
});
Run Code Online (Sandbox Code Playgroud)

基本上我期待我发送给服务器的数据是:

[name:test,key:foo,key2:bar]
Run Code Online (Sandbox Code Playgroud)

但我得到的是:

name=test&key=foo&key2=bar
Run Code Online (Sandbox Code Playgroud)

我错过了什么?如何将这些数据转换为JSON?

jquery json http-post

19
推荐指数
2
解决办法
6万
查看次数

如何在Playframework 2/Scala中使用WS API使用空体请求发布http帖子?

我尝试使用Play2/Scala WS API向服务端点发送HTTP POST请求.由于HTTP POST正文中没有要发送的参数,我该如何使用它发送

WS.url("http://service/endpoint).post()
Run Code Online (Sandbox Code Playgroud)

我试过post()没有争论,但它给了我一个错误.

无法将单元实例写入HTTP响应.尝试定义可写[单位]

你能帮忙吗?

提前致谢...

rest web-services scala http-post playframework-2.0

19
推荐指数
2
解决办法
4962
查看次数

使用Angular.js的HTTP POST

我是新手,我想使用Angular.js发出HTTP POST请求.我正在访问PHP脚本,其参数只是POST变量.从每个脚本返回的是JSON字符串.通常在HTML表单中,您可以提出如下请求:

<form method="post" action="url.php">
<input name="this">
<input name="that">
<input value="Submit">
</form>
Run Code Online (Sandbox Code Playgroud)

根据您的输入,单击提交后,JSON data1将返回如下内容: { "code" : 1 }

我无权访问脚本或托管它们的服务器.

我想知道Angular.js是否可以读取JSON数据1,将数据1与我们在JSON数据2中定义的内容相匹配,然后将它们输出到我的视图(<pre>data2</pre>).

例如,如果{ "code" : 1 }检索到,我希望我的JSON输出代码#1的值:

{ "code" : [
  { 1: "User already logged in." }, 
  { 2: "Wrong parameters, try again."}, 
  { 3: "etc., etc." }
 ] 
};
Run Code Online (Sandbox Code Playgroud)

这是我的尝试:

<form ng-controller="PhpCtrl" name="f1">
<input type="text" name="name">
<input type="text" name="password">
<pre ng-model="codeStatus">{{codeStatus}}</pre>
<input type="submit" ng-click="add()" value="Submit">
</form>

function PhpCtrl($scope, $http, $templateCache) {
    $scope.method = 'POST';
    $scope.url …
Run Code Online (Sandbox Code Playgroud)

javascript php json http-post angularjs

19
推荐指数
2
解决办法
6万
查看次数

XMLHttpRequest发布HTML表单

当前设置

我有一个像这样的HTML表单.

<form id="demo-form" action="POST" method="post-handler.php">
   <input type="text" name="name" value="previousValue"/>
   <button type="submit" name="action" value="dosomething">Update</button>
</form>
Run Code Online (Sandbox Code Playgroud)

我可能在页面上有很多这些表单.

我的问题

如何异步提交此表单而不是重定向或刷新页面?我知道怎么用XMLHttpRequest.我遇到的问题是在javascript中从HTML中检索数据然后放入一个post请求字符串.这是我目前用于zXMLHttpRequest`的方法.

function getHttpRequest() {
    var xmlhttp;
    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    } else {// code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }

    return xmlhttp;
}

function demoRequest() {
       var request = getHttpRequest();
       request.onreadystatechange=function() {
             if (request.readyState == 4 && request.status == 200) {
                   console.log("Response Received");
             }
       }
       request.open("POST","post-handler.php",true);
       request.setRequestHeader("Content-type","application/x-www-form-urlencoded");
       request.send("action=dosomething");
}
Run Code Online (Sandbox Code Playgroud)

例如,假设在demoRequest()单击表单的提交按钮时调用了javascript方法,如何从此方法访问表单的值,然后将其添加到XMLHttpRequest …

javascript forms ajax xmlhttprequest http-post

19
推荐指数
2
解决办法
7万
查看次数

如何在C#中模拟浏览器HTTP POST请求并捕获结果

假设我们有一个带有搜索输入表单的网页,它通过HTTP GET向服务器提交数据.这意味着服务器通过查询字符串接收搜索数据.用户可以看到URL,也可以自己初始化此请求(通过URL +查询字符串).

我们都知道.这是个问题.

如果此网页通过HTTP POST向服务器提交数据怎么办?用户如何自己初始化此请求?

我知道如何捕获HTTP POST(这就是网络嗅探器的用途),但我如何在C#代码中自己模拟这个HTTP POST请求呢?

c# http-post

18
推荐指数
1
解决办法
3万
查看次数

使用HTML表单时如何在HTTP请求正文中发送数据?

HTTP规范说POST请求可以包含任意数据体.

HTML form元素可以POST到URL并可能包含input元素,但这些input元素将变为查询字符串.

如何form在按下提交按钮时发送它发送的HTTP POST请求正文中的数据?

html forms http http-post

18
推荐指数
2
解决办法
2万
查看次数

从$ _POST中的json读取关联数组

我正在使用jQuery将json对象发布到我的php应用程序.

jQuery.post("save.php",JSON.stringify(dataToSend), function(data){ alert(data); });
Run Code Online (Sandbox Code Playgroud)

从萤火虫中拉出的json字符串看起来像这样

{ "data" : [ { "contents" : "This is some content",
        "selector" : "DIV.subhead"
      },
      { "contents" : "some other content",
        "selector" : "LI:nth-child(1) A"
      }
    ],
  "page" : "about_us.php"
}
Run Code Online (Sandbox Code Playgroud)

在PHP中我试图将其转换为关联数组.

到目前为止,我的PHP代码是

<?php
$value = json_decode(stripcslashes($_POST));
echo $value['page'];
?>
Run Code Online (Sandbox Code Playgroud)

对ajax调用的响应应为"about_us.php",但它返回空白.

php post json associative-array http-post

18
推荐指数
2
解决办法
7万
查看次数

在Laravel中,我怎样才能获得***POST参数?

我知道可以在Laravel中使用$request->get('my_param')Input::get('my_param')获取POST或GET请求参数(我现在正在使用v5/dev版本,但对于4.2来说它是相同的).

但是,如何my_param通过POST参数确保我的来自并且不仅仅来自?my_param=42附加到URL?(除了恢复到ol' $_POST$_GET超级全局并将可测试性抛出窗外)

(注意:我也知道该Request::get方法将为POST请求提供POST参数,如果POST和具有相同名称的URL/GET参数都存在,但是......但是如果参数通过url查询字符串登陆相反,我想用一种Laravel惯用法来了解这一点)

php http-post laravel laravel-request

17
推荐指数
1
解决办法
9773
查看次数

启用CORS AngularJS以发送HTTP POST请求

我想通过将表单提交到我的服务器来发送HTTP POST请求,该服务器位于不同的域中(并使用node.js在服务器脚本中启用了cors).

这是所有Angular配置都是的脚本:

var myApp = angular.module('myApp', ['ngRoute']);

myApp.config(function($routeProvider, $locationProvider, $httpProvider) {

  $httpProvider.defaults.useXDomain = true;
  delete $httpProvider.defaults.headers.common['X-Requested-With'];

  $routeProvider
  .when('/', {
    controller: 'RouteCtrl',
    templateUrl: 'views/home_views.html'
  })
  .when('/login', {
    controller: 'RouteCtrl',
    templateUrl: 'views/login_views.html'
  })
  .when('/register', {
    controller: 'RouteCtrl',
    templateUrl: 'views/register_views.html'
  })
});

myApp.controller("UserController", function($scope, $http) {
  $scope.formData = {};
  $scope.clickMe = function() {
    console.log("Yay");
      $http({
        method: 'POST',
        url: 'http://localhost:8183/user/register',
        data: $.param($scope.formData),
      })
      .success(function(data) {
        console.log(data);
        if(!data.success) {
          console.log("error here");
        } else {
          console.log("error there");
        }
      });
  }
}); ...
Run Code Online (Sandbox Code Playgroud)

我正在使用AngularJS 1.2.22,正如本教程中所述(启用CORS …

javascript http-post httprequest cors angularjs

17
推荐指数
1
解决办法
8万
查看次数

如何使用Android中的Volley库上传文件?

我已经有一个Request子类用于http发送到服务器.问题是,我不知道如何为文件添加参数.将字符串发布到服务器很容易.但我需要将文件添加为不同的参数.我该怎么做?

public class AddNewPetRequest extends Request<JSONObject> {

    private Response.Listener<JSONObject> listener;

    public AddNewPetRequest(String url, Map<String, String> params,
                                Response.Listener<JSONObject> reponseListener, Response.ErrorListener errorListener) {
        super(Request.Method.GET, url, errorListener);
        this.listener = reponseListener;
        this.params = params;
    }

    public AddNewPetRequest(int method, String url, Map<String, String> params,
                                Response.Listener<JSONObject> reponseListener, Response.ErrorListener errorListener) {
        super(method, url, errorListener);
        this.listener = reponseListener;
        this.params = params;
    }

    protected Map<String, String> getParams()
            throws com.android.volley.AuthFailureError {
        return params;
    };

    @Override
    protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
        try {
            String jsonString = new String(response.data,
                    HttpHeaderParser.parseCharset(response.headers));
            return Response.success(new …
Run Code Online (Sandbox Code Playgroud)

android file http-post httprequest android-volley

17
推荐指数
1
解决办法
5万
查看次数