0
私はWeb APIを作成してlocalhostに公開しました。httpが動作しているときに、httpの投稿要求がangularjsで動作しない
AngularJSを使用してスクリプトファイルを作成したWebページが作成されました。
apiとウェブページの両方がlocalhostで動作します。
私はHTTP取得リクエストを送信するとJSON応答が返されますが、POSTメソッドを使用してAPIにデータを追加することはできません。
投稿要求はPostmanで完全に機能します。新しいデータが追加されます。あなたはすべてのデータとJSONモデルを受け入れるエンティティモデルを作成する必要があり
app.jsのHTTP POSTの場合
var app = angular.module("myApp", []);
app.controller('myCtrl',function($scope,$http){
$scope.register = function() {
$http({
method: "POST",
url: 'http://localhost:8080/pool/api/employees/',
headers: { 'Content-Type': 'application/json' },
data:JSON.stringify({"PSID": "1236","Name": "Michael","Type": "Owner","Ph": "9585456211"})
});
}
$scope.search = function() {
url = 'http://localhost:8080/pool/api/employees/' + $scope.getid;
$http.get(url).success(function (data) {
$scope.msg = "";
alert(JSON.stringify(data));
$scope.id = data.PSID;
$scope.name = data.Name;
$scope.type = data.Type;
$scope.phone = data.Ph;
}).
error(function (data) {
$scope.msg = "No PSID found";
$scope.id = "";
$scope.name = "";
$scope.type = "";
$scope.phone = "";
})
}
});
ウェブアピコントローラコード
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.Description;
using webapi.Models;
namespace webapi.Controllers
{
public class EmployeesController : ApiController
{
private webapiContext db = new webapiContext();
// GET: api/Employees
public IQueryable<Employee> GetEmployees()
{
return db.Employees;
}
// GET: api/Employees/5
[ResponseType(typeof(Employee))]
public async Task<IHttpActionResult> GetEmployee(string id)
{
Employee employee = await db.Employees.FindAsync(id);
if (employee == null)
{
return NotFound();
}
return Ok(employee);
}
// PUT: api/Employees/5
[ResponseType(typeof(void))]
public async Task<IHttpActionResult> PutEmployee(string id, Employee employee)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
if (id != employee.PSID)
{
return BadRequest();
}
db.Entry(employee).State = EntityState.Modified;
try
{
await db.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!EmployeeExists(id))
{
return NotFound();
}
else
{
throw;
}
}
return StatusCode(HttpStatusCode.NoContent);
}
// POST: api/Employees
[ResponseType(typeof(Employee))]
public async Task<IHttpActionResult> PostEmployee(Employee employee)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
db.Employees.Add(employee);
try
{
await db.SaveChangesAsync();
}
catch (DbUpdateException)
{
if (EmployeeExists(employee.PSID))
{
return Conflict();
}
else
{
throw;
}
}
return CreatedAtRoute("DefaultApi", new { id = employee.PSID }, employee);
}
// DELETE: api/Employees/5
[ResponseType(typeof(Employee))]
public async Task<IHttpActionResult> DeleteEmployee(string id)
{
Employee employee = await db.Employees.FindAsync(id);
if (employee == null)
{
return NotFound();
}
db.Employees.Remove(employee);
await db.SaveChangesAsync();
return Ok(employee);
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
private bool EmployeeExists(string id)
{
return db.Employees.Count(e => e.PSID == id) > 0;
}
}
}
ブラウザコンソールに何かエラーがありますか?何をしようとしているのかを示すエラーやコードはなく、何が間違っているのかを知ることは不可能です。 – Claies
昨日は同じことが起こっていましたが、郵便配達員とはうまくいっていましたが、APIではなく、コードを投稿して見てください。 – Wcan
どのコンソールエラーが表示されますか? – Rakeschand