好吧,这是故事:
我有一些数据需要发送到服务器,但他们应该首先变成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?
我尝试使用Play2/Scala WS API向服务端点发送HTTP POST请求.由于HTTP POST正文中没有要发送的参数,我该如何使用它发送
WS.url("http://service/endpoint).post()
Run Code Online (Sandbox Code Playgroud)
我试过post()没有争论,但它给了我一个错误.
无法将单元实例写入HTTP响应.尝试定义可写[单位]
你能帮忙吗?
提前致谢...
我是新手,我想使用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) 当前设置
我有一个像这样的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 …
假设我们有一个带有搜索输入表单的网页,它通过HTTP GET向服务器提交数据.这意味着服务器通过查询字符串接收搜索数据.用户可以看到URL,也可以自己初始化此请求(通过URL +查询字符串).
我们都知道.这是个问题.
如果此网页通过HTTP POST向服务器提交数据怎么办?用户如何自己初始化此请求?
我知道如何捕获HTTP POST(这就是网络嗅探器的用途),但我如何在C#代码中自己模拟这个HTTP POST请求呢?
HTTP规范说POST请求可以包含任意数据体.
HTML form元素可以POST到URL并可能包含input元素,但这些input元素将变为查询字符串.
如何form在按下提交按钮时发送它发送的HTTP POST请求正文中的数据?
我正在使用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",但它返回空白.
我知道可以在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惯用法来了解这一点)
我想通过将表单提交到我的服务器来发送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 …
我已经有一个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)