【发布时间】:2017-03-08 08:33:36
【问题描述】:
我有以下json:
var jsondata = {
"id": 1,
"name": "Test Name",
"price": 100,
"city": "XYZ"
};
我想post/send这个json数据到一些url location(我有不同的URL locations,即无论urllocation我输入input字段,然后到那个url location我需要post/send这个json数据)点击Send按钮。我尝试了以下方法,但无法发送到给定的输入网址。
var app = angular.module('myApp', []);
app.controller('TestCtrl',function($scope, $http) {
$scope.sendToLocation = function(){
var jsondata = {
"id": 1,
"name": "Test Name",
"price": 100,
"city": "XYZ"
};
$.ajax({
type: 'POST',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
url: 'My URL HERE',
data: JSON.stringify(jsondata),
success: function(data) {
alert("yes, you have posted the data !");
}
});
//alert(JSON.stringify(jsondata));//gives the above json
};
});
<script src="https://code.angularjs.org/1.5.8/angular.js"></script>
<div ng-controller="TestCtrl">
<br>
<input type="url" maxlength="50" size="50" ng-model="sendToLocation.url"><br><br>
<button id="sendbutton" type="button" ng-click="sendToLocation()" >Send</button>
</div>
创建Fiddle。
请让我知道我做错了什么以及如何发送 json。提前致谢!
【问题讨论】:
标签: jquery angularjs json ajax angular-ngmodel