Javascript 如何测试我的web api Post web方法以了解';里面怎么了?
这就是我的情况 我们预先安装了Microsoft Dynamics CRM,我正在尝试从它向我在另一台服务器上创建的服务发出ajax调用。我已经解决了很多问题,但现在我能够通过我在CRM中的表单中添加的javascript成功地从CRM向我的服务发出GET请求 仅供参考(因为我现在还不能完全确定这些事情是否相关)Javascript 如何测试我的web api Post web方法以了解';里面怎么了?,javascript,c#,web-services,testing,asp.net-web-api,Javascript,C#,Web Services,Testing,Asp.net Web Api,这就是我的情况 我们预先安装了Microsoft Dynamics CRM,我正在尝试从它向我在另一台服务器上创建的服务发出ajax调用。我已经解决了很多问题,但现在我能够通过我在CRM中的表单中添加的javascript成功地从CRM向我的服务发出GET请求 仅供参考(因为我现在还不能完全确定这些事情是否相关) 我必须在IIS中为我的服务设置匿名身份验证(CRM 我将依赖它自己的身份验证) 我必须用CRM安装的主机地址设置Access Control Allow Origin的响应头 所以
- 我必须在IIS中为我的服务设置匿名身份验证(CRM 我将依赖它自己的身份验证)
- 我必须用CRM安装的主机地址设置Access Control Allow Origin的响应头
using CRMService.Models;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Net.Mail;
using System.Web;
using System.Web.Http;
namespace CRMService.Controllers
{
public class DefaultController : ApiController
{
// GET: Default
public string Get()
{
return "Hi";
}
[HttpPost]
public string GiveParameters(TestClass tc)
{
try
{
Dictionary<string, object> Parameters = new Dictionary<string, object>();
Parameters.Add("id", tc.id);
Parameters.Add("one", tc.one);
Parameters.Add("two", tc.two);
NonQuery("InsertTestItem", ConfigurationManager.ConnectionStrings["TestConnection"].ToString(), Parameters);
return "success";
}
catch (Exception ex)
{
return "ex";
}
}
}
}
var new_budget = Xrm.Page.data.entity.attributes.get("new_budget").getValue();
var new_name = Xrm.Page.data.entity.attributes.get("new_name").getValue();
var id = Xrm.Page.data.entity.getId();
data = '{"TestClass":{"one":"' + new_name + '", "two":"'+ new_budget +'", "id":"'+ id +'"}}'
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "https://<hiddingMyUrl>/api/Default/GiveParameters",
data: data,
dataType: "json",
success: function(data) {
console.log("Success");
},
error: function(result) {
console.log("Error");
}
});
您应该尝试使用REST客户端。 这里有两个不错的:
我个人更喜欢邮递员,但实际上两者都很好。我试着在《提琴手》里混了一会儿。我已经用过很多次小提琴手了,我对它很满意。然而,考虑到回复的标题和诸如此类的内容,我无法得到一篇文章,甚至无法通过。好吧,使用postman,我从我的文章中得到了415-无冲突的媒体类型。“此资源不支持请求实体的媒体类型‘text/plain’。”我查找了该错误并找到了这篇文章:我看到一些关于CORS内容的讨论,内容类型仍然需要设置为application/json,并且在CORS情况下有一个特定的头允许这样做。但是我找不到该标题。我认为您应该添加以下标题:Accept:application/json Content Type:application/json您链接了我链接的同一篇文章。;)我添加了这些标题,但仍然得到415。返回此消息:此资源不支持请求实体的媒体类型“text/plain”。
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { action = "Index", id = UrlParameter.Optional }
);
}