通过$ http角度服务将大字符串传递给api控制器

时间:2014-08-13 09:16:08

标签: ajax angular-services

我有大字符串,现在我想通过$ http角度服务将数据传递到api控制器。我失去了很多次。

这是我的大字符串

var strObj="{\"countryName\":null,\"cityName\":null,\"stateName\":null,\"objectID\":-1,\"id\":0,\"locationID\":1,\"companyName\":\"\",\"companyShortName\":\"\",\"yearEstablished\":0,\"companyTypeID\":0,\"noOfEmployee\":null,\"regNo\":\"\",\"cBINo\":null,\"yearlyRevenue\":null,\"tINNo\":\"\",\"vATNo\":\"\",\"phone\":\"\",\"mobile\":null,\"fax\":null,\"email\":\"\",\"webSite\":\"\",\"houseNo\":\"\",\"flat\":null,\"section\":null,\"block\":null,\"street\":null,\"cityID\":0,\"stateID\":null,\"countryID\":0,\"zip\":\"\",\"pOBox\":null,\"directories\":null,\"license\":null,\"status\":1,\"isGroupOfCompany\":1,\"isCompanyBranch\":1,\"insertUserID\":100000001,\"editUserID\":null,\"lastUpdate\":\"1899-12-31T18:00:00.000Z\",\"isSelected\":false,\"isEnabled\":false,\"companyMiscList\":[],\"contactPersonList\":[{\"countryName\":null,\"cityName\":null,\"stateName\":null,\"contactPersonName\":null,\"contactDetails\":null,\"objectID\":-2,\"id\":1,\"locationID\":1,\"contactPersonRefID\":-1,\"contactPersonBankID\":0,\"contactPersonRefCode\":1,\"titleID\":0,\"firstName\":\"\",\"middleName\":null,\"lastName\":\"\",\"genderID\":0,\"designation\":\"\",\"workPhone\":\"\",\"homePhone\":null,\"fax\":null,\"mobile\":\"\",\"email\":\"\",\"houseNo\":\"\",\"flat\":null,\"section\":null,\"block\":null,\"street\":null,\"cityID\":0,\"stateID\":null,\"countryID\":0,\"zip\":null,\"isMainContact\":0,\"insertUserID\":100000001,\"editUserID\":null,\"lastUpdate\":\"1899-12-31T18:00:00.000Z\",\"isSelected\":false,\"isEnabled\":false}],\"billingAddressList\":[{\"countryName\":null,\"cityName\":null,\"stateName\":null,\"addressType\":null,\"contactType\":null,\"contactPersonName\":null,\"contactPersonMobile\":null,\"contactPersonEmail\":null,\"addressDetails\":null,\"objectID\":-3,\"id\":2,\"locationID\":1,\"addressRefID\":0,\"addressBillingID\":-1,\"addressShippingID\":0,\"addressRefCode\":1,\"addressTypeID\":100000040,\"houseNo\":\"\",\"flat\":null,\"section\":null,\"block\":null,\"street\":null,\"cityID\":0,\"stateID\":null,\"countryID\":0,\"phone\":\"\",\"fax\":null,\"mobile\":\"\",\"zip\":\"\",\"email\":\"\",\"pOBox\":null,\"contactPersonID\":0,\"contactTypeID\":0,\"insertUserID\":100000001,\"editUserID\":null,\"lastUpdate\":\"1899-12-31T18:00:00.000Z\",\"isSelected\":false,\"isEnabled\":false}],\"shippingAddressList\":[{\"countryName\":null,\"cityName\":null,\"stateName\":null,\"addressType\":null,\"contactType\":null,\"contactPersonName\":null,\"contactPersonMobile\":null,\"contactPersonEmail\":null,\"addressDetails\":null,\"objectID\":-4,\"id\":3,\"locationID\":1,\"addressRefID\":0,\"addressBillingID\":0,\"addressShippingID\":-1,\"addressRefCode\":1,\"addressTypeID\":100000041,\"houseNo\":\"\",\"flat\":null,\"section\":null,\"block\":null,\"street\":null,\"cityID\":0,\"stateID\":null,\"countryID\":0,\"phone\":\"\",\"fax\":null,\"mobile\":\"\",\"zip\":\"\",\"email\":\"\",\"pOBox\":null,\"contactPersonID\":0,\"contactTypeID\":0,\"insertUserID\":100000001,\"editUserID\":null,\"lastUpdate\":\"1899-12-31T18:00:00.000Z\",\"isSelected\":false,\"isEnabled\":false}],\"bankList\":[{\"countryName\":null,\"cityName\":null,\"stateName\":null,\"objectID\":-5,\"id\":4,\"locationID\":1,\"bankRefID\":-1,\"bankRefCode\":0,\"bankName\":\"\",\"branchName\":null,\"houseNo\":\"\",\"flat\":null,\"section\":null,\"block\":null,\"street\":null,\"cityID\":0,\"stateID\":null,\"countryID\":0,\"zip\":\"\",\"pOBox\":null,\"acctName\":null,\"acctNumber\":\"\",\"swiftCode\":null,\"iBANNumber\":null,\"aBANumber\":null,\"phone\":\"\",\"mobile\":null,\"fax\":null,\"email\":\"\",\"webSite\":null,\"insertUserID\":100000001,\"editUserID\":null,\"lastUpdate\":\"1899-12-31T18:00:00.000Z\",\"isSelected\":false,\"isEnabled\":false,\"contactPersonList\":[]}]}"

我已经编写了发送

的代码
            $http({
                url: remoteService + '/SaveDraft',
                method: 'POST',
                data: $.param({ jsonData: strObj }),
                headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' },
                //params: { jsonData: strObj },                    
                //timeout: d,
                //cache: d,
                //transformRequest: d,
                //transformResponse: d
            }).then(function (results) {
                return results;
            }).catch(function (e) {
                throw e;
            });

这是我的api控制器方法

    [HttpPost]
    public int SaveDraft(string jsonData)
    {
        try
        {
            return 1;
        }
        catch (Exception)
        {
            throw;
        }
    }

我的代码有什么问题。请帮我。感谢

1 个答案:

答案 0 :(得分:2)

如果你想传递一个大字符串,因为你必须使用两个方法,一个是javascript,另一个是api方法。

java脚本方法看起来像这样

               var data = {                    
                Data: strObj
            };

           $http({
                url: url,
                method: 'POST',
                data: data,
                headers: { 'Content-Type': 'application/json; charset=UTF-8' },
                //params: { jsonData: "I am shohel rana" },
                //timeout: 10,
                //cache: false,
                //transformRequest: false,
                //transformResponse: false
            }).then(function (success) {
                return success;
            }).catch(function (e) {
                throw e;
            });

和api方法是

    [HttpPost]
    public bool SaveDraft([FromBody]object draft)
    {
        try
        {

        }
        catch (Exception)
        {
            throw;
        }
    }