A new visual servoing with obstacle avoidance method is presented in this paper. The basic idea of the method is to solve a constraint optimization problem using a sequential quadratic programming (SQP) method to get each control step. The objective function of the problem is a least squares of errors between the end-effector and the moving target in the image plane, and the constraint is used to ensure the end-effector keeping a safe distance from the obstacle in the visual servoing process. SQP methods are commonly used in solving static optimization problems. We extend an SQP method for dynamic target tracking problems. Besides, we employ a recursive least squares algorithm to estimate the composite image Jacobian, which maps the velocity of joint variables into the velocity of image features, online. Thus, our method does not require the priori knowledge of intrinsic and extrinsic parameters of the cameras and the kinematics model of the robot. By using this method, the end-effector of the robot can track the moving target more accurately and avoid obstacle more safely than traditional obstacle avoidance methods which use the potential function. A two-degrees-of-freedom robot with a fixed camera is simulated to test the method. The simulation results validate the effective of the method.