在AngularJS中使用$ http Post调用方法

时间:2017-06-09 08:44:58

标签: angularjs asp.net-mvc

我正在尝试在AngularJS中调用ProcessCriteria方法,但出于某种原因,我不断收到错误消息:

  

VM18010:1 POST http://example.com/api/TalentPool/ProcessCriteria 404   (未找到)

以下是我的来电代码:

    var param = { 'Item': item.Key, 'SolrLabel': SolrLabel };

        $http({
            method: 'POST',
            url: '/api/TalentPool/ProcessCriteria',
            data: param
            //headers: {
            //    'Content-Type': 'application/x-www-form-urlencoded'
            //}
        }).then(function (response) {
                // success
                console.log('Facet Data Posted');
                return response;
                                    },
                function (response) { // optional
                    // failed
                    console.log('facet post error occured!');
                });

我的服务器端方法:

  [System.Web.Http.HttpPost]
        public IHttpActionResult ProcessCriteria(string Item, string SolrLabel)
        {

            var itm = Item;
            var solr = SolrLabel;

            return Ok();
        }

有什么建议吗?

3 个答案:

答案 0 :(得分:1)

ASP.net无法与Route Table中的请求匹配,因为您的操作中有2个参数,路由器无法理解。

它期望一个数据对象,你的参数会变形。

首先,制作一个类似的模型:

public  class Criteria
{
    public string Item { get; set; }
    public string SolrLabel { get; set; }
}

然后改变你的行动:

[System.Web.Http.HttpPost]
public IHttpActionResult ProcessCriteria(Criteria criteria)
{
    var itm = criteria.Item;
    var solr = criteria.SolrLabel;
    return Ok();
}

更新

并使用JSON.stringify更新您的javaScript部分:

var param = { 'Item': item.Key, 'SolrLabel': SolrLabel };

        $http({
            method: 'POST',
            url: '/api/TalentPool/ProcessCriteria',
            data: JSON.stringify(param)
            //headers: {
            //    'Content-Type': 'application/x-www-form-urlencoded'
            //}
        }).then(function (response) {
                // success
                console.log('Facet Data Posted');
                return response;
                                    },
                function (response) { // optional
                    // failed
                    console.log('facet post error occured!');
                 });

答案 1 :(得分:0)

你可以按照上面的答案创建一个类,你可以像http这样在

中传递数据
 var obj = {
            url: url,
            async: true,
            method: 'POST',
           headers: {
                 "content-type": "application/json; charset=utf-8",
               }
            };
            if (typeof data != 'undefined' || typeof data != null) {
                obj.data = data;
            }
            $http(obj).then(function(response){
            },function(error){
            });

答案 2 :(得分:0)

我让我工作,下面是其他人的代码,如果他们被卡住了。

    private void createPDF(ArrayList<Searchresult> alllist)  {

            com.itextpdf.text.Document doc= new com.itextpdf.text.Document(PageSize.A4,50,50,50,50);
            try {
                String USER_PASS = "hasu123";
                String OWNER_PASS = "vnr123";
                 String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Dir";

            File dir = new File(path);
            if(!dir.exists())
                dir.mkdirs();

            File file = new File(dir, "newFile.pdf");
                FileOutputStream fOut = new FileOutputStream(file);
                PdfWriter writer=PdfWriter.getInstance(doc, fOut);

              /*  writer.setEncryption(USER_PASS.getBytes(), OWNER_PASS.getBytes(),
                        PdfWriter.ALLOW_PRINTING, PdfWriter.ENCRYPTION_AES_128);*/
                HeaderAndFooter event = new HeaderAndFooter();
                writer.setPageEvent(event);

                //open the document
                doc.open();
               /* PdfReader reader = new PdfReader(path);
                PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(path));
                stamper.setEncryption(USER_PASS.getBytes(),USER_PASS.getBytes(),
                        PdfWriter.ALLOW_PRINTING, PdfWriter.ENCRYPTION_AES_128 | PdfWriter.DO_NOT_ENCRYPT_METADATA);
                stamper.close();
                reader.close();*/

                PdfPTable table = new PdfPTable(8);
                Font subtitleFont = FontFactory.getFont("Times Roman",11, BaseColor.BLUE);
                Font subtitle = FontFactory.getFont("Times Roman",7, BaseColor.BLACK);

                PdfPCell pcell = new PdfPCell(new Phrase("Stoneid",subtitleFont));
                pcell.setHorizontalAlignment(com.itextpdf.text.Element.ALIGN_CENTER);
                table.addCell(pcell);
                PdfPCell pcell2 = new PdfPCell(new Phrase("Shape",subtitleFont));
                pcell2.setHorizontalAlignment(com.itextpdf.text.Element.ALIGN_CENTER);
                table.addCell(pcell2);
                PdfPCell pcell3 = new PdfPCell(new Phrase("Carat",subtitleFont));
                pcell3.setHorizontalAlignment(com.itextpdf.text.Element.ALIGN_CENTER);
                table.addCell(pcell3);
                PdfPCell pcell4 = new PdfPCell(new Phrase("Color",subtitleFont));
                pcell4.setHorizontalAlignment(com.itextpdf.text.Element.ALIGN_CENTER);
                table.addCell(pcell4);
                PdfPCell pcell5 = new PdfPCell(new Phrase("Clarity",subtitleFont));
                pcell5.setHorizontalAlignment(com.itextpdf.text.Element.ALIGN_CENTER);
                table.addCell(pcell5);
                PdfPCell pcell6 = new PdfPCell(new Phrase("Cut",subtitleFont));
                pcell6.setHorizontalAlignment(com.itextpdf.text.Element.ALIGN_CENTER);
                table.addCell(pcell6);
                PdfPCell pcell7 = new PdfPCell(new Phrase("Polish",subtitleFont));
                pcell7.setHorizontalAlignment(com.itextpdf.text.Element.ALIGN_CENTER);
                table.addCell(pcell7);
                PdfPCell pcell8 = new PdfPCell(new Phrase("Symm",subtitleFont));
                pcell8.setHorizontalAlignment(com.itextpdf.text.Element.ALIGN_CENTER);
                table.addCell(pcell8);
                table.setHeaderRows(1);

                for (int i=0;i<alllist.size();i++){
                    String stoneid=alllist.get(i).getStoneid();
                    String shape=alllist.get(i).getShape();
                    String carat=alllist.get(i).getSize();
                    String color=alllist.get(i).getColor();
                    String cut=alllist.get(i).getCut();
                    String polish=alllist.get(i).getPolish();
                    String symmm=alllist.get(i).getSym();
                    String clarity=alllist.get(i).getClarity();
                    if(color.equalsIgnoreCase("")){
                        color="*"; // this fills the cell with * if the String is empty otherwise cell won't be created
                    }
                    if(stoneid.equalsIgnoreCase("")){
                        stoneid="*"; // this fills the cell with * if the String is empty otherwise cell won't be created
                    }
                    if(shape.equalsIgnoreCase("")){
                        shape="*"; // this fills the cell with * if the String is empty otherwise cell won't be created
                    }
                    if(carat.equalsIgnoreCase("")){
                        carat="*"; // this fills the cell with * if the String is empty otherwise cell won't be created
                    }
                    if(color.equalsIgnoreCase("")){
                        color="*"; // this fills the cell with * if the String is empty otherwise cell won't be created
                    }
                    if(cut.equalsIgnoreCase("")){
                        cut="*"; // this fills the cell with * if the String is empty otherwise cell won't be created
                    }
                    if(symmm.equalsIgnoreCase("")){
                        symmm="*"; // this fills the cell with * if the String is empty otherwise cell won't be created
                    }
                    if(polish.equalsIgnoreCase("")){
                        polish="*"; // this fills the cell with * if the String is empty otherwise cell won't be created
                    }
                    if(clarity.equalsIgnoreCase("")){
                        clarity="*"; // this fills the cell with * if the String is empty otherwise cell won't be created
                    }
                    table.addCell(createCel(stoneid,subtitle,com.itextpdf.text.Element.ALIGN_CENTER));
                    table.addCell(createCel(shape,subtitle,com.itextpdf.text.Element.ALIGN_CENTER));
                    table.addCell(createCel(carat,subtitle,com.itextpdf.text.Element.ALIGN_CENTER));
                    table.addCell(createCel(color,subtitle,com.itextpdf.text.Element.ALIGN_CENTER));
                    table.addCell(createCel(cut,subtitle,com.itextpdf.text.Element.ALIGN_CENTER));
                    table.addCell(createCel(symmm,subtitle,com.itextpdf.text.Element.ALIGN_CENTER));
                    table.addCell(createCel(polish,subtitle,com.itextpdf.text.Element.ALIGN_CENTER));
                    table.addCell(createCel(clarity,subtitle,com.itextpdf.text.Element.ALIGN_CENTER));
                }
                doc.add(table);
            } catch (DocumentException de) {
                Log.e("PDFCreator", "DocumentException:" + de);
            } catch (IOException e) {
                Log.e("PDFCreator", "ioException:" + e);
            }
            finally {
                doc.close();
            }
           viewPdf("newFile.pdf", "Dir");
        }


 private PdfPCell createCel(String s, Font subtitle, int alignCenter) {
        PdfPCell pdfPCel = new PdfPCell(new Phrase(s,subtitle));
        pdfPCel.setHorizontalAlignment(alignCenter);
        return pdfPCel;
    }


    private void viewPdf(String s, final String dir) {
        new Thread() {
            public void run() {
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                }
                runOnUiThread(new Runnable() {
                    public void run() {
                        File file = new File(Environment.getExternalStorageDirectory() + "/" + dir + "/" + "newFile.pdf");
                        Intent target = new Intent(Intent.ACTION_VIEW);
                        //target.setPackage("com.adobe.reader");
                        target.setDataAndType(Uri.fromFile(file),"application/pdf");
                        target.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
                        Intent intent = Intent.createChooser(target, "Open File");

                        try {
                            startActivity(intent);
                        } catch (ActivityNotFoundException e) {
                            // Instruct the user to install a PDF reader here, or something
                            Toast.makeText(ResultDataNew.this, "No PDF Viewer is install", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
            }
        }.start();
    }