FormData Append上的非法调用错误

时间:2018-11-21 16:27:32

标签: jquery ajax

我将Javascript对象和文件附加到formData上,如下所示:

Color[] Data;
SpriteRenderer SpriteRenderer;

GameObject ColorPicker;
GameObject Selector;
BoxCollider Collider;
public GameObject target;
Ray rayray;
private Plane MyPlane;

public int Width { get { return SpriteRenderer.sprite.texture.width; } }
public int Height { get { return SpriteRenderer.sprite.texture.height; } }

public Color Color;

void Awake()
{

    ColorPicker = transform.Find("ColorPicker").gameObject;
    SpriteRenderer = ColorPicker.GetComponent<SpriteRenderer>();
    Selector = transform.Find("Selector").gameObject;
    Collider = ColorPicker.GetComponent<BoxCollider>();

    Data = SpriteRenderer.sprite.texture.GetPixels();

    Color = Color.white;

    Debug.Log(Collider);
    MyPlane = new Plane(transform.TransformDirection(Vector3.forward), transform.position);
}


void Update()
{

    if (Input.GetMouseButton(0))
    {
        rayray = Camera.main.ScreenPointToRay(Input.mousePosition);
        MyPlane = new Plane(transform.TransformDirection(Vector3.forward), transform.position);

        Vector3 screenPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        screenPos = new Vector3(screenPos.x, screenPos.y);


        //check if we clicked this picker control
        RaycastHit[] ray = Physics.RaycastAll(rayray.origin, rayray.direction);
        foreach (RaycastHit h in ray)
        {
            Debug.Log(h.collider.name);
            if (h.collider.name == "ColorPicker")
            {
                Selector.transform.position = screenPos;

                //get color data
                screenPos -= ColorPicker.transform.position;
                int x = (int)(screenPos.x * Width);
                int y = (int)(screenPos.y * Height) + Height;

                if (x > 0 && x < Width && y > 0 && y < Height)
                {
                    Color = Data[y * Width + x];
                    target.GetComponent<Renderer>().material.color = Color;
                    Debug.Log(Width);
                    Debug.Log(Height);
                }
            }
        }
    }
}

当我这样拨打AJAX时:

public class Main {

    public static void main(String[] args) {
        System.out.println(convert("4200000000000000", 4, 3));
    }

    static String convert(String number, int firstDigits, int lastDigits) {
        String first = number.substring(0, firstDigits);
        String middle = number.substring(firstDigits, number.length() - lastDigits).replaceAll("0", ".");
        String last = number.substring(number.length() - lastDigits, number.length());

        return first + middle + last;
    }
}

我收到非法调用错误。我应该如何将文件追加到formData以便避免此错误?谢谢。

1 个答案:

答案 0 :(得分:1)

尝试不使用json内容类型和processData:

var myFile = document.getElementById('myinput').files[0];
    myFormData.append('myfile', myFile);

$.ajax({
   type: "POST",
   url: "/SomeController",
   async:false,
   data: myFormData,
   contentType: false,
   processData: false
});