阿贾克斯语法错误。无效字符

时间:2019-03-28 07:02:37

标签: javascript ajax asp.net-mvc-4

我有一个asp.net mvc应用程序。我有一个名为“ Access”的视图,控制器,我正在尝试在ajax中调用controllers方法之一。而且我遇到了语法错误。根本没有调用方法测试

控制器方法代码:

[HttpGet]
public JsonResult Test(string p)
{            
    return Json(new User() {  Name="Nat"}, JsonRequestBehavior.AllowGet);
}

ajax通话:

$.ajax({
       type: "GET",
       url: "Access/Test", // the method we are calling
       contentType: "application/json; charset=utf-8",
       data: { "p": "test" },
       dataType: "json",
        success: function (result) {
                alert("yes");
                alert('Yay! It worked!' + result);                
        },
        error: function (request, status, error) {
                alert('Not worked ' + error);
        }

});

这是一个非常简单的代码,但是我不能强迫它工作。我想了解为什么我的json数据不正确并且出现错误。我想执行我的方法

2 个答案:

答案 0 :(得分:0)

您发送了 import { Component } from '@angular/core'; import { IonicPage, NavController, NavParams } from 'ionic-angular'; import { DomSanitizer, SafeHtml } from '@angular/platform-browser'; @IonicPage() @Component({ selector: 'page-mountain-infos', templateUrl: 'mountain-infos.html', }) export class MountainInfosPage { model1: any; public Mountains = [ { MountainName : 'Everest', desc: 'The Mount Everest was discovered in 1841 . ', height : '8848 meters' }, ] constructor(public navCtrl: NavController, private _sanitizer: DomSanitizer) {} autocompleteListFormatter = (data: any): SafeHtml =>{ let html = `<span>${data.MountainName}</span>`; return this._sanitizer.bypassSecurityTrustHtml(html); } } 请求,因此请删除CREATE TABLE student(Id integer PRIMARY KEY, Name text); INSERT INTO student VALUES(1,'Tom'); INSERT INTO student VALUES(2,'Lucy'); INSERT INTO student VALUES(3,'Frank'); INSERT INTO student VALUES(4,'Jane'); INSERT INTO student VALUES(5,'Robert'); CREATE TABLE Qualifications(Id integer PRIMARY KEY, qualification_text text, qualification_level integer); INSERT INTO Qualifications VALUES(1,'SSC',1); INSERT INTO Qualifications VALUES(2,'HSC',2); INSERT INTO Qualifications VALUES(3,'Grad',3); CREATE TABLE StudentQualifications(student_id integer, qualification_id integer, marks float); INSERT INTO StudentQualifications VALUES(1,1,80); INSERT INTO StudentQualifications VALUES(1,2,90); INSERT INTO StudentQualifications VALUES(1,3,90); INSERT INTO StudentQualifications VALUES(2,1,75); INSERT INTO StudentQualifications VALUES(3,1,70); SELECT result.Name, highest.qualification_text, highest_marks.marks,lowest.qualification_text,lowest_marks.marks FROM (SELECT Std.name AS Name,Std.Id AS stud_id, MAX(q.qualification_level) AS highest,MIN(q.qualification_level) AS lowest FROM student std LEFT JOIN StudentQualifications sq ON std.id = sq.student_id INNER JOIN Qualifications q ON sq.qualification_id = q.Id GROUP BY std.Name, std.Id) result INNER JOIN Qualifications highest ON highest.qualification_level = result.highest INNER JOIN Qualifications lowest ON lowest.qualification_level = result.lowest INNER JOIN StudentQualifications highest_marks ON highest.id = highest_marks.qualification_id AND result.stud_id = highest_marks.student_id INNER JOIN StudentQualifications lowest_marks ON lowest.id = lowest_marks.qualification_id AND result.stud_id = lowest_marks.student_id; GET-data请求中不需要它们:

contentType

并从操作中删除GET,或将其设置为null:

$.ajax({
       type: "GET",
       url: "Access/Test", // the method we are calling
       dataType: "json",
       success: function (result) {
                alert("yes");
                alert('Yay! It worked!' + result);                
        },
        error: function (request, status, error) {
                alert('Not worked ' + error);
        }

});

答案 1 :(得分:-1)

  1. AccessContoller.cs

    public class AccessController : Controller
    {
      [HttpGet]
      public JsonResult Test(string p)
      {            
         return Json(new User() {  Name="Nat"}, JsonRequestBehavior.AllowGet);
      }
    }
    
  2. 索引或任何页面调用

    $(document).ready(function(){
       $.ajax({
        type: "GET",
        url: "Access/Test", //First Controller Name(Access) or After Method Name
        contentType: "application/json; charset=utf-8",
        data: { "p": "test" },
        dataType: "json",
        success: function (result) {
            alert("yes");
            alert('Yay! It worked!' + result);                
        },
        error: function (request, status, error) {
            alert('Not worked ' + error);}
       });
    });